当用户未登录时设置_POST_THMBILAILE

时间:2013-06-26 作者:egdavid

我目前正在使用set_post_thumbnail() 函数将以编程方式上载的图像插入帖子。问题是,仅当用户登录时,该功能才能正常工作。

如果用户未登录,wordpress会在后端表示该文件已附加到帖子,但不会将其显示为特色图像。我在那个函数中没有任何条件语句。

有人有过同样的问题吗?这是正常行为吗?

编辑:注意到在我将Wordpress从3.5.1升级到3.5.2后出现了这个问题,但我真的不知道它是否相关

编辑:这是我的代码示例

// This part is for testing purpose, the file is well placed with the right permission
$dir = \'/home/web/tmpdir\';
$data = $_POST[\'imageCanvas\'];
$preview = base64_decode( str_replace( \'data:image/png;base64,\', \'\', $data ) );
$file = \'000011.png\'; 
$filename = $dir . \'/\' . $file;
file_put_contents( $filename, $preview );

// Here I copy the test file directly into the wordpress uploads dir
// After this line I find the file in the uploads dir, so, let\'s say that for now it works as intended
$wp_filetype = wp_check_filetype( basename( $file ), null );
copy( $filename, \'/home/web/wordpress/wp-content/uploads/\' . basename( $file ) );
$attachment = array(
    "post_mime_type" => $wp_filetype[\'type\'],
    "post_title" => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $file ) ),
    "post_content" => \'\',
    "post_status" => \'inherit\'
);

// This part worked well until I recently upgraded WP to 3.5.2...
$attach_id = wp_insert_attachment( $attachment, $file, $lastpostID );
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $lastpostID, $attach_id );

// This part returns the right ids in Apache logs...
error_log( \'attachid: \' . $attach_id . \' - postid: \' . $lastpostID );
我可以在wordpress媒体库中找到他帖子的附件:

Wordpress media library

但是如果我打开帖子,我看不到任何特色图片。

Post featured image

1 个回复
最合适的回答,由SO网友:egdavid 整理而成

这是W3 Total cache的一个问题。由于数据库缓存仅为未登录用户激活,因此它不再按预期工作。

现在奇怪的部分。。。数据库缓存被激活了几个月,一直运行良好,直到我将WP升级到3.5.2(可能同时升级到W3TC)。

结束