Pat是正确的REST API是向站点添加新帖子/页面的最佳方式。下面是一个更完整的指南,其中有一个示例:https://rudrastyh.com/wordpress/rest-api-create-delete-posts.html#create_post
发布新帖子的示例代码:
$api_response = wp_remote_post( \'https://WEBSITE/wp-json/wp/v2/posts\', array(
\'headers\' => array(
\'Authorization\' => \'Basic \' . base64_encode( \'LOGIN:PASSWORD\' )
),
\'body\' => array(
\'title\' => \'My test\',
\'status\' => \'draft\', // ok, we do not want to publish it immediately
\'content\' => \'lalala\',
\'categories\' => 5, // category ID
\'tags\' => \'1,4,23\' // string, comma separated
\'date\' => \'2015-05-05T10:00:00\', // YYYY-MM-DDTHH:MM:SS
\'excerpt\' => \'Read this awesome post\',
\'password\' => \'12$45\',
\'slug\' => \'new-test-post\' // part of the URL usually
// more body params are here:
// developer.wordpress.org/rest-api/reference/posts/#create-a-post
)
) );
$body = json_decode( $api_response[\'body\'] );
// If the post was published successfully we can display a little message
if( wp_remote_retrieve_response_message( $api_response ) === \'Created\' ) {
echo \'The post \' . $body->title->rendered . \' has been created successfully\';
}
正如您所看到的,您只需通过body参数为post传递数据。
Authorization:
<第3行的em>登录:密码是具有发布后功能的网站用户的用户名和密码对。更好的授权方法可能是使用应用程序密码插件和设置令牌。您可以在此处找到插件:
https://wordpress.org/plugins/application-passwords/