对于每个和我面临同样困境的人来说,实际上有很多在线资源指导如何解决这个问题。我发现找到解决方案的最佳方法实际上是通过编程方式搜索类似导入WordPress附件的内容,这会产生大量的解决方案。
感谢@tobbr为我指出了正确的方向。我的最终解决方案是创建一个插件,然后遍历旧站点(CakePHP)数据库以查找旧图像及其路径,然后使用这些路径将附件导入WordPress。
这里要考虑的重要关键点是,我将旧站点图像目录克隆到WordPress上载目录中。旧图像路径如下所示:
CAKE_ROOT/app/WebRoot/images/2012/10/2/12345/image_name.jpg
其中12345=数据库中图像项的ID(导入WordPress时,它将成为附件的ID。因此,我将上面的整个图像文件夹移动到WordPress uploads目录中,因此我的WordPress图像文件夹结构如下所示:
WP_ROOT/wp-content/uploads/2012/10/2/12345/image_name.jpg
需要注意的是,在您开始以编程方式导入附件之前,您的图像必须位于WordPress上载目录中的某个位置(任意位置)。还要确保您拥有必要的权限,为了导入,我运行了chmod -R 777
开始之前,请在“我的上载文件夹”上执行命令。
然后,我编写了插件,它并没有我想象的那么复杂,所以这里是为需要执行类似任务的任何人准备的:
<?php
/*
Plugin Name: Image Importer
Plugin URI:
Description:
Author: xxxxx
Version: 1.0
Author URI:
*/
error_reporting(E_ALL);
add_action( \'admin_menu\', \'sd_plugin_menu\' );
function sd_plugin_menu() {
add_options_page( \'My Plugin Options\', \'My Image Importer\', \'manage_options\', \'sd-image-importer\', \'sd_image_importer\' );
}
function sd_image_importer() {
if ( !current_user_can( \'manage_options\' ) ) {
wp_die( __( \'You do not have sufficient permissions to access this page.\' ) );
}
echo \'<div style="overflow:hidden;padding:20px;margin:20px 0 0 0;border:1px solid #ccc;font-size:14px;color:#666;">\';
if($_REQUEST[\'getfiles\'] == 1) {
// Make a MySQL Connection
$con = mysqli_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysqli_select_db($con,"xxxx") or die(mysqli_error($con));
// Retrieve all the data from the images table
$result = mysqli_query($con,"SELECT * FROM cake_attached_images WHERE created > \'2012-10-01\' AND created < \'2012-10-02\'") or die(mysql_error($con));
// pre buffer empty array so as not to create one on each iteration of DB
$files = array();
while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)) {
$year = date(\'Y\', strtotime($row[\'created\']));
$month = date(\'m\', strtotime($row[\'created\']));
$day = ltrim(date(\'d\', strtotime($row[\'created\'])),0);
$id = $row[\'id\'];
$foreign_key = $row[\'foreign_key\'];
$caption = $row[\'caption\'];
$filename = $row[\'filename\'];
$files[$id] = array(
\'id\' => $id,
\'foreign_key\' => $foreign_key,
\'path\' => "$year/$month/$day/$id/$filename",
);
};
}; // End if getfiles param present in URL
if($_REQUEST[\'insert\'] == 1){
global $wpdb;
foreach($files as $file){
$wp_filetype = wp_check_filetype(basename($wp_upload_dir[\'baseurl\'].\'/\'.$file[\'path\']), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
\'guid\' => $wp_upload_dir[\'baseurl\'].\'/\'. $file[\'path\'],
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($wp_upload_dir[\'baseurl\'].\'/\'.$file[\'path\'])),
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'post_parent\' => $file[\'foreign_key\']
);
// Insert attachment, newly added attachmnet ID is returned...
$attach_id = wp_insert_attachment( $attachment, $wp_upload_dir[\'path\'].\'/\'. $file[\'path\'], $file[\'foreign_key\'] );
// The file MUST be included to make use of the wp_generate_attachment_metadata function below
require_once(ABSPATH . \'wp-admin/includes/image.php\');
// Generate metadata from newly added attachment above
$attach_data = wp_generate_attachment_metadata( $attach_id, $wp_upload_dir[\'path\'].\'/\'. $file[\'path\'] );
// Updates atttachment metadata for newly added attachmnet based on the generated metadata above
if(wp_update_attachment_metadata( $attach_id, $attach_data )){
// Optional, can be sued to set the added attachmnet as the featured image on the post
add_post_meta($file[\'foreign_key\'], \'_thumbnail_id\', $attach_id, true);
// Simple outout to indicate if attachmnet was added or not, for debugging
echo \'<span style="color:green;">Attachment successfully added for post with ID <strong>\'.$file[\'foreign_key\'].\'</strong>, path of new attachment is at <strong>\'.$wp_upload_dir[\'baseurl\'].\'/\'.$file[\'path\'].\'</strong></span><br/>\';
}else{
echo \'<span style="color:red;">Failed to generate new attachment for post with ID <strong>\'.$file[\'foreign_key\'].\'</strong></span><br/>\';
}
}
} // end if insert param present in URL request
$files = null;
echo \'</div>\';
}?>
<小时>
AND HERE IS A BONUS FUNCTION:
我在编写插件后遇到了这个问题,但是如果你只有几个的话(我有超过100000张图片要导入!)WordPress中有一个非常方便的功能,名为media\\u sideload\\u image,它可以从远程URL获取图像并将其附加到您选择的帖子上,非常值得一看。在codex获取更多信息:
http://codex.wordpress.org/Function_Reference/media_sideload_image<小时>
Some links to WordPress Codex resources that helped me are as follows:
- http://codex.wordpress.org/Function_Reference/wp_insert_attachment -以编程方式插入附件http://codex.wordpress.org/Function_Reference/wp_update_attachment_metadata - 更新新添加附件的元数据add\\u post\\u meta-如果要将新添加的图像设置为特色图像
Other external sites that aided me:
- http://www.zdnet.com/blog/diy-it/programmatically-importing-thousands-of-featured-image-post-thumbnails-into-wordpress/118 - ZDNethttp://nlb-creations.com/2012/09/26/how-to-programmatically-import-media-files-to-wordpress/ - NB创作Programmatically get images by URL and save in uploads folder - WordPress答案Programatically creating image attachments from local URLs and setting featured image - WordPress的回答是:我希望这能像帮助我一样帮助别人。西蒙