我正在尝试使用RESTAPI创建一个post。我可以通过下面的获取请求来执行此操作。OurPostData
包含标题、内容和URL of a featured image 这是一个外部url
fetch(\'https://mywebste.online/wp-json/wp/v2/post\', {
method: \'POST\',
credentials: \'same-origin\',
headers: new Headers({
\'Content-Type\': \'application/json;charset=UTF-8\',
\'X-WP-Nonce\': qrAjax.nonce
}),
body: JSON.stringify(OurPostData),
}).then(response => {
console.log(response);
return response.json();
}).then(data => console.log(data));
});
除特色图像外,此操作有效。如果我能把它传递给回调函数,使用下面的代码可以实现吗?add_action(\'wp_ajax_code_post_create\', \'code_post_create_callback\');
但我不知道怎么通过。这个data
在我的代码中有post id.(如何在中访问它code_post_create_callback
?)我可以使用以下命令保存文件
$file = \'https://externalwebsite.com/image.jpg\';
$file_array = [ \'name\' => wp_basename( $file ), \'tmp_name\' => download_url( $file ) ];
// If error storing temporarily, return the error.
if ( is_wp_error( $file_array[\'tmp_name\'] ) ) {
return $file_array[\'tmp_name\'];
}
// Do the validation and storage stuff.
require_once (\'wp-load.php\');
require_once (\'wp-admin/includes/admin.php\');
$id = media_handle_sideload( $file_array, 0, $desc );
//var_dump($id);
// If error storing permanently, unlink.
if ( is_wp_error( $id ) ) {
@unlink( $file_array[\'tmp_name\'] );
return $id;
}
我怎么打电话code_post_create_callback
? 如何访问data
在这个回调中?