raz921992
Forum Replies Created
Viewing 5 replies - 1 through 5 (of 5 total)
-
@lutheradams I have the latest version but as @arnaudbroes said I think we have another scenario here that could happen.
@arnaudbroes thank you for response!
Great, thank you!
Hello, I have the same problem. Still have this problem.
Forum: Developing with WordPress
In reply to: API Rest – How to add images inside post and feature image?Yes this is also a problem but the content-type needs to be a general format(application/json). I read multiple topics on internet and I managed to do it right.
This is the good version of how to create a post with WP API add feature image and one image on gallery:
//connection to wordpress api $username = 'user'; $password = 'password'; $client = new Client([ 'base_uri' => 'https://www.localhost/wp-json/wp/v2/', 'headers' => [ 'Authorization' => 'Basic ' . base64_encode($username . ':' . $password), 'Accept' => 'application/json', 'Content-type' => 'application/json', 'Content-Disposition' => 'attachment', ] ]); $this->createCategoriesIfNotExist($client); // uploading featured image to wordpress media and getting id $imageOnMedia = $client->post( 'media', [ 'multipart' => [ [ 'name' => 'file', 'contents' => file_get_contents($data['imageUrl']), 'filename' => $data['fileName'], ], ], 'query' => [ 'status' => 'publish', 'title' => $data['originalFileName'], 'comment_status' => 'closed', 'ping_status' => 'closed', 'alt_text' => $data['fileName'], 'description' => '', 'caption' => '', ], ]); $media = json_decode($imageOnMedia->getBody(), true); $categoryId = $client->get('categories', ['query' => ['slug' => $data['deviceCategory']]]); if(!empty($categoryId)) { $wpcategory = json_decode($categoryId->getBody()); } else { $wpcategory = 1; } $mediaStructure = '<!-- wp:gallery {"ids":['.$media['id'].'],"imageCrop":false,"linkTo":"attachment"} --> <figure class="wp-block-gallery columns-1"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><a href="'.$media['link'].'"><img src="'.$media['source_url'].'" alt="werwerwer-1574671862.jpg" data-id="'.$media['id'].'" data-full-url="'.$media['source_url'].'" data-link="'.$media['link'].'" class="wp-image-'.$media['id'].'"/></a></figure></li></ul></figure> <!-- /wp:gallery -->'; // uploading post to wordpress with featured image id $post = $client->post( 'posts', [ 'multipart' => [ [ 'name' => 'title', 'contents' => $data['postTitle'], ], [ 'name' => 'content', 'contents' => $mediaStructure ], [ 'name' => 'featured_media', 'contents' => $media['id'] ], ], 'query' => [ 'status' => 'publish', 'categories' => [$wpcategory[0]->id] ] ]);
Thank you @anant101289 for your reply!
Please delete this topic message. I made a mistake it’s not the right topic. Sorry!
Viewing 5 replies - 1 through 5 (of 5 total)