Jim*_*son 6 php wordpress image
我正在尝试以编程方式添加一堆帖子,然后添加它们的附带图像。我有一个包含所有图像文件名的数组,并且我已经能够将它们添加到数据库中,但我似乎无法创建正确的缩略图。
这些帖子来自位于 wp-content/uploads/dirname/ 的 csv。它们的文件名中包含与 CSV 中的 ID 相对应的数字,这就是我如何知道需要将哪些图像添加到哪个帖子 ID 中。
我已经获得了 wp_insert_attachment() 部分来处理它们自己的小目录中的图像,但我无法生成缩略图。我安装了重新生成缩略图插件,并且它能够生成它们,但我似乎无法让它以编程方式发生。
我认为这可能是因为 wp_generate_attachment 需要照片位于 /uploads/2011/12/ (例如)中,所以我开始沿着移动图像然后尝试添加它们的路径。无论如何,这是有道理的,因为我有点想制作副本,而不是将 5 或 6 个不同的媒体尺寸添加到我的 wp-content/uploads/dirname/ 目录中。
无论如何,这是行不通的。通过 PHP 的副本移动图像不起作用,并且不会生成缩略图。
foreach ($frazerfiles[$stock_num] as $stock_img){
require_once(ABSPATH . 'wp-admin/includes/image.php');
echo "... ...Trying to add attachment metadata...";
// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
if (copy($file_to_move, $uploads['path']."/")) {
echo "Moved $file_to_move to ".$uploads['path']."/";
$my_moved_file = $uploads['path']."/".$stock_img;
// I think this is failing because the images aren't in the upload dir.
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
'post_content' => '',
'post_status' => 'inherit'
);
if ($attach_id = wp_insert_attachment( $attachment, $my_moved_file, $newpostid )) {
if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
echo "...success for $my_moved_file: ID:$attach_id<br />\n";
} else {
echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
print_r($attach_data);
}
} else { // inserting attachment failed
echo "Insert attachment failed for $my_moved_file to $newpostid<br />\n";
}
} else {
echo "Failed moving $file_to_move to ".$uploads['path']."/";
}
}// images foreach
Run Code Online (Sandbox Code Playgroud)
wp_update_attachment_metadata
在与 WordPress 核心文件进行交叉检查后,唯一遗漏的似乎是wp_generate_attachment_metadata
.
尝试添加
wp_update_attachment_metadata($attach_id, $attach_data);
Run Code Online (Sandbox Code Playgroud)
后
echo "...success for $my_moved_file: ID:$attach_id<br />\n";
Run Code Online (Sandbox Code Playgroud)
所以 if 块看起来像
if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
echo "...success for $my_moved_file: ID:$attach_id<br />\n";
wp_update_attachment_metadata($attach_id, $attach_data);
} else {
echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
print_r($attach_data);
}
Run Code Online (Sandbox Code Playgroud)
建议:(与问题无关)
移动线
require_once(ABSPATH . 'wp-admin/includes/image.php');
Run Code Online (Sandbox Code Playgroud)
for 循环之外(上方)。
更新 1:添加解决复制问题的建议。您错过了“检查文件类型”行。(如果目标文件($my_moved_file
)已经存在,PHP复制功能将失败)
更改此代码
// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
if (copy($file_to_move, $uploads['path']."/")) {
echo "Moved $file_to_move to ".$uploads['path']."/";
$my_moved_file = $uploads['path']."/".$stock_img;
// I think this is failing because the images aren't in the upload dir.
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
'post_content' => '',
'post_status' => 'inherit'
);
Run Code Online (Sandbox Code Playgroud)
到
// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
$my_moved_file = $uploads['path']."/".$stock_img;
if (copy($file_to_move, $my_moved_file)) {
echo "Moved $file_to_move to ".$my_moved_file;
// Check the file type
$wp_filetype = wp_check_filetype(basename($my_moved_file), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($my_moved_file)),
'post_content' => '',
'post_status' => 'inherit'
);
Run Code Online (Sandbox Code Playgroud)