admin管理员组文章数量:1279148
I'm trying to post content with wp_remote_post. Content that I want to be posted is in a separate php file, which echo's block with html and WPBakery shortcode. I tried include but it didn't work, I tried file_get_contents but it posts 1 in the content as if there were no errors executing the code.
Code that I use:
$postDataContent = "blahblahblah";
$api_response = wp_remote_post( 'https://SITE/wp-json/wp/v2/posts', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password )
),
'body' => array(
'title' => $title,
'status' => 'draft',
'content' => $postDataContent,
'categories' => 6,
'date' => $published,
'slug' => $id
)
) );
$body = json_decode( $api_response['body'] );
The question is about 'content' => "$postDataContent". If I declare $postDataContent = "blahblahblah"; it will post "blahblahblah" on created pages. But instead I need it to post content of the separate php file. Is there a way to do so? Thanks.
EDIT
Can you include your attempt? It's difficult too debug code that hasn't been shared
This is the main part of the code I'm concerned about:
$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
'date' => '2021-05-05T10:00:00', // YYYY-MM-DDTHH:MM:SS
'slug' => 'new-test-post' // part of the URL usually )
) );
$body = json_decode( $api_response['body'] );
It works fine. LOGIN:PASSWORD uses pair from application passwords. It creates a new post with specified title, slug, category. But what I want to do is to post different content into 'content'. So, instead of 'lalala' it will be getting content from another php file. That file has some php and html in it. My thoughts: create a variable and assign to that variable another php file. I tried:
ob_start();
include "fileWithContent.php";
$postDataContent = ob_get_clean();
But it doesn't work.
include
doesn't work either.
Maybe there is another way to create a post in WordPress and add content from the separate php file?
本文标签: apiPost content in wpremotepost
版权声明:本文标题:api - Post content in wp_remote_post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741295440a2370790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论