将图像上传到wordpress网站的代码 .
function upload_image($file, $post_id) {
require_once('../wp-admin/includes/image.php');
require_once('../wp-admin/includes/file.php');
require_once('../wp-admin/includes/media.php');
//require_once(ABSPATH . 'wp-admin/includes/media.php');
//require_once(ABSPATH . 'wp-admin/includes/file.php');
//require_once(ABSPATH . 'wp-admin/includes/image.php');
// upload image to server
media_sideload_image($file['url'], $post_id);
// get the newly uploaded image
$attachments = get_posts( array(
'post_type' => 'attachment',
'number_posts' => 1,
'post_status' => null,
'post_parent' => $post_id,
'orderby' => 'post_date',
'order' => 'DESC',)
);
// returns the id of the image
return $attachments[0]->ID;
}
$result1 = attach_image_url('http://www.americanmattress.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/p/a/parmelee_fm2.jpg','0');
print_r($result1);
这是问题所在:
-
函数位于RootWPFolder / Somefolder / ThisFunction.php中
-
所需文件位于RootWPFolder / wp-admin / includes / file.php
我收到以下错误:
致命错误:require_once():无法打开所需的'ABSPATHwp-admin / includes / media.php'(include_path =' . :/ opt / alt / php54 / usr / share / pear:/ opt / alt / php54 / usr /共享/ PHP的')
1 回答
错误消息
建议没有定义ABSPATH . 在根目录中打开
wp-load.php
,并确保已设定 . 如果是这样,那么使用这些是安全的而不是相对目录: