WP REST attachment - wordpress

How do I attach images and videos on my post via WP REST API?
I was trying via /wp-json/wp/v2/posts/3372/meta including form-data key and value (images) but returns the following
{
"code": "rest_forbidden",
"message": "Sorry, you cannot view this post.",
"data": {
"status": 401
}
}

WPAPI Official has a good article about this.
You need to do the following two steps.
Upload media via REST API. This is done in a POST request, for more info, see this answer .
If you wish to set the uploaded media as featured image, include the media id in featured_media field in your POST request, see this doc.
If you wish to attach image in to content of a post, then you'll need to create an image tag in content html. Like
<img src="linkToUploadedMedia">

Related

Create product with external attachment URL using REST API

I am following documentation for create a product, https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product .
I want to create a product with the images which are saved in an external server.
when I use the following code the API uploading the src image to the current server,
'images' => [
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
],
]
But I don't want to upload those images to the current server.
If I explain it in WordPress code without API,
1. I need to create parent post ( post type - post )
2. create another post with post type "attachment", content as external URL and parent post as above post's ID
But is there any chance to use the above woocommerce API for this requirement?

LinkedIn Share API not showing image for URL some of the time

Our application lets an admin create a suggested social post for all of the members of his or her organization. The admin will insert a URL in our system and our platform creates a unique URL for each member. This is a link with a 302 redirect to the original link. It works similar to Bit.ly. When the member gets the suggested post he/she can create his/her own content for the post and url and approve for posting via the LinkedIn API. That all works perfectly.
As an example the share url could be https://example.com
Member one gets: https://yip.sh/1234
Member two gets: https://yip.sh/4321
Member three gets: https://yip.sh/4444
The PROBLEM: Some (a minority) of the member's posts do not share the meta image for the url. The majority of the social posts work and show a meta image so that tells me both the 302 redirect and the destination url are working correctly. I also have tried the urls from the post that do not work in the LinkedIn post inspector and the meta image shows up there.
My ASSUMPTION: Sometimes pulling the meta image takes a little longer than LinkedIn wants to wait and since each URL is unique it needs to check each time.
Possible SOLUTION: Automated ping of Linkedin post inspector to cache image before the share. This was what we did with Facebook when we had a similar problem. I have not found a way to do that though with LinkedIn.
Just to show the code we are using to share the post. But as I mentioned above this is working a majority of the time.
pass_the_hash = {
"author": "urn:li:person:#{#social_identity.uid}",
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"text": "My Comment"
},
"shareMediaCategory": "ARTICLE",
"status" => "READY",
"originalUrl" => "https://yip.sh/1234"
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
}
Has anyone seen a similar issue with LinkedIn Share API not showing meta image consistently? Or possibly know of a way to automate usage of the post inspector?
To quote LinkedIn...
After updating your website with perfect preview content and image, if you try sharing your website link you will still see old preview content and image. That's because LinkedIn caches link preview content for 7 days. Source: Official LinkedIn Documentation.
As you and they state, you can use the LinkedIn Post Inspector to refresh the cache. If you wanted to go the approach of pinging/curling LinkedIn, you can do...
https://www.linkedin.com/sharing/share-offsite/?url={yourlURL}
For instance, seems to work for getting info from the LinkedIn share API for GitHub.
Of course, you do also have the less-desirable option of waiting 7 days, although it is nice to know that it eventually works itself out by itself.
In the ugcPosts endpoint of LinkedIn shares API, there are some changes that are causing these issues. You will need to make changes to your code and add extra key for thumbnails like below:
{
"author": "'.$ownerURN.$page_id.'",
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"text": "'.$text.'"
},
"shareMediaCategory": "ARTICLE",
"media": [
{
"status": "READY",
"description": {
"text": "'.$linkDescription.'"
},
"originalUrl": "'.$href.'",
"title": {
"text": "'.$linkTitle.'"
},
"thumbnails": [{
"url": "'.$imageHref.'"
}]
}
]
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
}
I hope this is useful to people who are facing issues with the linkedIn Shares API now. I know this question is old but we faced this issue for a few days and the API documentation is not pointing out the issues.

Wordpress pass post header & body to sendNotification.php

I have an entire post notification system built using firebase. Using a program like postman, I can send the title & body and the notifications show up on the iOS/android devices. So the entire linkage works. Specifically in postman using the Post function, the URL posted too is https://example.com/backend/sendNotification.php. The body code is:
{
"title" : "post title",
"body" : "post body"
}
But now I have to tie it into wordpress. The goal is that when a post is published, the title and body, most likely truncated to a limited amounts of characters, get passed to the URL.
I know the code will go in the function.php file, but beyond that I'm stumped. How can I pass the title & body to the URL?
First, you want to hook into the post creation. There are several hooks that would probably fit your needs. Then use wp_remote_post or curl to post your data to the webhook.
add_filter( 'wp_insert_post_data', 'push_post_data' );
function push_post_data( $data ) {
if ('publish' == $data['post_status']) {
wp_remote_post($url, $array_with_title_and body);
}
}
If you're using a child theme or a custom theme functions.php is fine.

How to fix non existent field in Gatsby GraphQL?

I use Wordpress, Gatsby + GraphQL and I want to build a blog. Unfortunately all blog posts needs to have all "features" for example featured image.
The problem is when I remove all posts. So my blog is now empty but gatsby build fails because it expects featured image to exists. The same problem is if I have posts but they do not have featured image. If I want to run gatsby successfully I need at least one post with featured image.
Example GraphQL query
{
allWordpressPost {
    edges {
      node {
        id
        title
        content
        featured_media {
          source_url
        }
      }
    }
  }
}
Error
GraphQLError: Cannot query field "featured_media" on type "wordpress__POST".
I'm worried because my only solution is to create a dummy post with all these things. What if I want to create a blog for my client and he deletes this dummy post? Is there a better way?
I don't have much experience with gatsby so I would be happy for suggestions.
Thank you
I had a similar issue that I was able to work around, but without seeing more of your code this is just a guess. When you render the data in your Post template file you can do a "not null" check. It could look something like {data.featured_media !== null ? (<img src={{ data.source_url}} /> ) : null }

Ghost API not adding post body

Having an issue where when using the following payload with a post request on https://localhost:2368/ghost/api/v0.1/posts the body of the post is not loaded into ghost blog.
{
title: 'some title',
html: '<p>some html</p>',
plaintext: 'some plaintext'
}
After the request, only the title is loaded into ghost blog.
Using Ghost v2.13.1
Would be helpful discover field(s) that should be used for the body of a post.
#mattcameron Ghost uses mobiledoc (spec) - the html and plaintext fields are generated from that and can't be set separately.
The v0.1 API is private and not documented as it wasn't designed for use outside of the admin area, if you can I suggest you wait a little while for the v2 Admin API which will be released soon along with SDKs to make it easier to work with.

Resources