Php 尝试以编程方式生成WordPress缩略图

Php 尝试以编程方式生成WordPress缩略图,php,image,wordpress,Php,Image,Wordpress,我试图以编程方式添加一堆帖子,然后添加它们的助理图像。我有一个包含所有图像文件名的数组,我可以将它们添加到数据库中,但似乎无法创建正确的缩略图 帖子来自wp content/uploads/dirname/中的csv。他们的文件名中有与CSV中的ID相对应的数字,这就是我如何知道哪些图像需要添加到哪个帖子ID中的方式 我已经让wp_insert_attachment()部分处理它们自己的小目录中的图像,但是我无法生成缩略图。我安装了“重新生成缩略图”插件,它能够生成缩略图,但我似乎无法通过编程实

我试图以编程方式添加一堆帖子,然后添加它们的助理图像。我有一个包含所有图像文件名的数组,我可以将它们添加到数据库中,但似乎无法创建正确的缩略图

帖子来自wp content/uploads/dirname/中的csv。他们的文件名中有与CSV中的ID相对应的数字,这就是我如何知道哪些图像需要添加到哪个帖子ID中的方式

我已经让wp_insert_attachment()部分处理它们自己的小目录中的图像,但是我无法生成缩略图。我安装了“重新生成缩略图”插件,它能够生成缩略图,但我似乎无法通过编程实现

我想这可能是因为wp_generate_attachment需要照片位于/uploads/2011/12/(例如),所以我开始移动图像,然后尝试添加它们。这是有道理的,因为我有点想复制,而不是有5或6个不同的媒体大小添加到我的wp内容/uploads/dirname/dir

不管怎样,它不起作用。通过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
foreach($frazerfiles[$stock\u num]作为$stock\u img){
require_once(ABSPATH.'wp admin/includes/image.php');
echo“…正在尝试添加附件元数据…”;
//将文件复制到上载目录
$uploads=wp_upload_dir();
$file_to_move=ABSPATH.“wp content/uploads/”$stock_img;
if(复制($file_to_move,$uploads['path'].“/”)){
echo“将$file_移动到”.$uploads['path']。“/”;
$my_moved_file=$uploads['path']。“/”$stock_img;
//我认为这是失败的,因为图像不在上传目录中。
$attachment=array(
'post_mime_type'=>$wp_filetype['type'],
“post\u title”=>preg\u replace('/\..[^.]+$/','',basename($stock\u img)),
'发布内容'=>'',
“post_状态”=>“继承”
);
如果($attach\u id=wp\u insert\u attachment($attachment,$my\u moved\u file,$newposted)){
如果($attach_data=wp_generate_attachment_metadata($attach_id,$my_moved_file)){
echo“…成功获取$my\u moved\u file:ID:$attach\u ID
\n”; }否则{ echo“…未能获取$my\u moved\u file ID:$attach\u ID
\n”; 打印(附上数据); } }else{//插入附件失败 echo“将$my\u文件的插入附件移动到$newpostid时失败
\n”; } }否则{ echo“无法将$file_移动到”.$uploads['path']。“/”; } }//foreach图像
在与WordPress核心文件交叉检查后,唯一似乎遗漏的是
wp\u更新\u附件\u元数据
紧接着
wp\u生成附件\u元数据

尝试添加

wp_update_attachment_metadata($attach_id, $attach_data);
之后

在for循环的外部(上方)

更新1: 添加修复复制问题的建议。 你漏掉了“检查文件类型”一行。 (如果目标文件(
$my\u moved\u 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'
);


嗯,你说得对。谢谢你的建议。不过,复制部分仍然不起作用。有什么想法吗?更新后的答案,请在更新后检查1:我最终将复制内容移动到了自己的功能中,它现在可以工作了。问题在于PHP的copy()与shell中的cp不同,因为第二个参数需要是目标目录中的文件名,而不仅仅是目录名。所以path/to/file/filename.ext不仅仅是path/to/file是的,关于复制你是正确的,这就是为什么我将
$my_moved_file
行移到上面的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);
  }
require_once(ABSPATH . 'wp-admin/includes/image.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'
);
// 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'
    );