so I am trying to use WP-API plugin:
i figured out how to get all posts for custom post type:
http://domain/wp-json/posts?type[]=customType
and i figured out how to get attachments:
http://domain/wp-json/media/
Now the problem is how to get images attached to single post. if I look at the soruce it seems to suggest a path like:
http://domain/wp-json/media/<postId>/
but I get an error:
[{"code":"json_post_invalid_type","message":"Invalid post type"}]
any Idea how to get this to work?
You can use the "parent" id argument, e.g:
$ curl http://demo.wp-api.org/wp-json/wp/v2/media?parent=<id>
where is the post's ID. What we do is ignore all versions of images except the original, which is found by looking for the GUID > rendered value, e.g:
guid: {
rendered: "http://demo.wp-api.org/wp-content/uploads/image-belonging-to-this-post.png"
}
As of this moment this functionality does not seem to be supported probably will be when the plugin goes into core but for now it is as is.
I ended up making my own json API to serve my needs by making the page templates recive emit JSON data.
Just an update to this old post. WP-API now supports getting media related to a particular post.
http://v2.wp-api.org/reference/media/
Code:
$ curl http://demo.wp-api.org/wp-json/wp/v2/media/<id>
Related
I'm trying to automate posting entries to a blog every time a specific action happens on my website.
I've been looking at the Wordpress documentation but I've only seen that I can put as the structure of the post. I would be interested in creating entries with text and images. Would it be possible to do this?
The text and images would be taken from the web page.
I'm using php with Laravel and the blog is in wordpress.
You can send a POST request to the "post" endpoint to POST a new post (no pun intended) (docs).
Although the content parameter is said to be an object in the docs, you can pass a HTML string to it like <h1>Some cool event happened</h1><p>See this picture</p><img src="https://some-path-to-picture.com/picture.png">
It seems you can even add images along with your content to be hosted on your wordpress site (see this Github question)
I am using php to pull in posts from a WP site using the REST API, and it gives you both options to use full content, and excerpt. Although the excerpt is just too short for what I am using it for. I also don't want to change the global excerpt length, just for this one project using the REST API. Is there anyway to change the excerpted length via REST API and not effect the rest of the website?
The only thing I can think of is truncating the full content via small script when pulling in the data from the REST API and creating my own "read more" link using other variable that are accessible like source_url.
I am new to REST API in wordpress, but I don't see anything related to it in the official documentation, so wasn't sure if someone knew a good trick, or if I'll just have to go the manual route. Thanks!
The excerpt length is just used to limit the length of the output in your template. So you should handle it's length in your WordPress template, more specifically in your functions.php not in your REST API.
See here for more info:
http://smallenvelop.com/limit-post-excerpt-length-in-wordpress/
Unfortunately there's no way to set the excerpt length for your pulling request, like $api-getposts(300) or something like that, since the excerpt is strongly tied to your WordPress site.
However, you could register an api-callback on your WP-site, which sets the excerpt to a higher value, pull the data, and reset it.
https://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length
Just wrap this function in a register_rest_route
So your request would look something like this
$api->setExcerpt(300);
$api->getData();
$api->setExcerpt(100);
I am developing an android app. I dont know anything about wordpress database or how to get data from the website. I googled for how to get data from the wordpress database I have got to know I could get json response from JSON API plugin of the wordpress. but It's hard for me to understand.
I am using food bakery theme for wordpress.
I just want to know how do I get required json response from this website. And where the data of products in that website is located in database.
for example : product,categories etc. etc
My expected response is something like this
{"Category":[{"ProductSubGroupCategoryCode":"PSG03","ProductGroupCategoryCode":"PG01","Description":"WRAPS"
,"CHello SirreatedBy":"101","CreatedDate":"\/Date(1486116932060)\/","ModifiedBy":null,"ModifiedDate":null}]},
{"SubCategory":[{"ProductID":"01","Description":"VEG BURGER","ReferanceID":"VEG BURGER","UOM":"EA","AlterUOM":"EA",
"ProductGroupCategoryCode":"PG01","ProductSubGroupCategoryCode":"PSG01","TaxCategoryCode":"","TaxCode":"VAT5","BuyPrice":100,
"AttributeCode":"PA01","BasicPrice":190.48,"TaxRate":5.00,"SalesPrice":200.00,"ProductImage":null,"CurrentStock":10,
"PlantCode":"PC01","StoreLocationCode":"S01","DisplayOnPOS":true,"POSDisplayOrder":0,"CreatedBy":"101",
"CreatedDate":"\/Date(1486446663333)\/","ModifiedBy":"101","ModifiedDate":"\/Date(1486447253063)\/"}
Edit:
I tried with WP REST API i have come upto, when i use this link it gives me response with id 1 http://localhost/mysite/wp-json/wp/v2/posts/1.
when when I have tried with other post IDs it gives me blank response.
FYI I got post ID for that certain post from wordpress_posts table of wordpress database
if you want to get post:
https://example.com/wp-json/wp/v2/posts
if you want to get a particular post
https://example.com/wp-json/wp/v2/posts/1852
whereas 1852 is the id of the post
For getting JSON response from word press site you need to install certain open source plugin on word press site
here is an WP API link
Click here
As per documentation You can also create custom API
You can access your posts json on wordpress with
http://oursite.com/wp-json/wp/v2/posts
I want to use the WordPress JSON api to fetch content for a mobile app. I can iterate over the posts I receive - and I notice there is a "Rendered" field in the posts. I want to display that to the mobile user. Generally in a scrolling list view. However that rendered content is missing all styling CSS information. I'd like to embed that JSON API "rendered" content in web-views without fetching each of the whole posts like www.example.com/customtype/slug1 in WebView1 then the same for slug2 in WebView2 etc. I mean - I already fetched the content part - just missing the styles. Mobile data is precious :)
Is there a way to fetch the CSS information for a post in the active theme via the REST API? I tried caching style info in a global var as discussed in this SO answer (which I verified in debug). Then adding a custom REST endpoint to fetch the stuff I stored in that global variable. This almost works. The endpoint works. The data is cached after I display any post. However the global var in the child theme required to make this work appears to not be in the same scope as the plugin where I'm putting my rest to mobile stuff (that same global is always null/empty in the plugin).
My thoughts are now to make a custom post display in my child theme that just emits only the wp_head() stuff and I then fetch&cache that in the app - but that seems awkward. But I'm new to WP and PHP so - here we are. Thanks for any help!
I'm currently using this JSON to get the latest flickr photos of an ID:
http://api.flickr.com/services/feeds/photos_public.gne?id=49107890#N06&tagmode=any&format=json&jsoncallback=?
Now I need to change my code to display a set instead of an ID. I can get some JSON return with this:
http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=3bfff97a6e1eb0b1a0a7d460c780e273&photoset_id=72157623801339634&per_page=6&format=json
But there are no source URLs? I'm hoping someone with a little more flickr API experience than I can help out. I simply need to supply a Set ID and for it to return the latest 6 thumbnails.
You can construct the URLs yourself using the guide here: http://www.flickr.com/services/api/misc.urls.html
All the information needed is returned in the JSON response:
http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
or
http://farm{farm-id}.static.flickr.com/{server-id}/{id}{secret}[mstb].jpg
or
http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
As phidah pointed out, you can easily construct the URL from the returned data. However, there's a much easier method that just requires adding an extra parameter to your REST request. Just append &extras=url_sqor you can use any of the following, depending on which URL you want:
url_sq : Small Square
url_t : Thumbnail
url_s : Small
url_m : Medium
url_o : Original (requires special handling)
And you'll get an extra field in your JSON response that contains the URL. Easier in my opinion. :)
Check here for a full list of fields you can add.