我正在创建一个自定义的帖子表单,该表单将获取外部图像并将其上载到媒体库,然后将其附加到该帖子。这应该可以通过使用media\\u sideload\\u image实现,但我得到的是一个空白页面,而不是正常工作,也没有错误。我的代码:
// Add the content of the form to $post as an array
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'tags_input\' => $tags,
\'post_type\' => \'link_submit\',
\'post_status\' => \'publish\'
);
//save the new post and return its ID
if ($stop == false){
$pid = wp_insert_post($new_post);
update_post_meta($pid,\'Link\',$link,true);
update_post_meta($pid,\'Type\',$type,true);
// attach image
set_time_limit(300);
$upload = media_sideload_image($link, $pid, "test");
if ( is_wp_error( $upload ) ){
die( \'Nope\' );
}
$link = get_permalink( $pid );
wp_redirect( $link."?posted" );
die();
}
想法?谢谢,丹尼斯
编辑:新代码:
if (is_page(\'Upp\')){
require_once(ABSPATH . \'wp-admin/includes/file.php\');
require_once(ABSPATH . \'wp-admin/includes/media.php\');
set_time_limit(300);
$upload = media_sideload_image("http://29.media.tumblr.com/tumblr_lmp1tbiYwh1qzlfumo1_500.jpg", "202", "my description");
die ( $upload );
}