Posting with Wordpress API REST - php

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)

Related

Is it possible to preview a wordpress post remotely without publishing it?

I am struggling with remote post preview using wordpress api.
Currently I am publishing new post, capturing html and deleting post.
This works, but I am looking for better solution.
I have an app which is publishing posts remotely to Wordpress. I need to add functionality of post preview on somebody's blog which will be viewed in my app. I need to get full html of wordpress site with post content which is held in my app. I am publishing post using this: http://developer.wordpress.com/docs/api/1.1/post/sites/%24site/post‌​s/… capturing html and deleting this post. This solution has lot of disadvantages, for example when wordpress user inform his readers about new post via email, readers will get a notification about new post that doesn't exists any more.

Generate link to clone or duplicate a post from wordpress frontend

I've found a number of plugins for wordpress that add a link to the table in the backend to clone or duplicate a post or page (Duplicate Post, Clone Post, and Post Duplicator). This is nice, but I'm building a bit of an app on wordpress where I'm using a custom post type as an entry and would like to be able to quickly copy an entry with a link from the frontend of the site (similar to how edit_post_link works but without jumping into the backend).
I'm using Gravity Forms for the create and edit functionality for entries on the frontend, but I can't quite figure out the best approach for a secure way to submit a request, execute the clone query, and the return to the same page I was on before (i.e. not jumping into the backend). Gravity Forms Post Updates plugin does something similar with a do_action call to generate a link. I've looked through this function and generally get what's happening, but I'm not sure if I need all of the extra plugin class structure.
I was hoping to find an example of a function that could create a secure url with a nonce and then another function we validate the url and execute the query and return to the previous page. Is this the right way to go or am I barking up the wrong tree? If so, any code samples or examples that might be able to help get me started?
Much appreciated!
I found this post (http://rudrastyh.com/wordpress/duplicate-post.html) and was able to figure out what I needed to. Instead of adding a filter to include the link in the backend, I instead created an action that can be called with do_action and generates the link. I also have a page and page template that is solely for executing the call and then redirecting back to the referring url.

Wordpress WP-API get images attached to post

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>

create a content-only page for mobile news feed in modx

I've been tasked with providing the backend for a news feed that will be used by our company apps. The feed will pull articles from our current website, which is built with ModX (evolution). So far, I've designed the feed to send JSON through a specified url containing the needed information. It's currently in the following format (using Ditto placeholders):
{
"title":"[+longtitle+]",
"description":"[+description+]",
"link":"[(site_url)][~[+id+]~]"
},
Here's my issue - the link I'm providing through the JSON (in the link tag) opens the full, desktop version of the page. Our current site is not responsive, and was not originally designed to handle mobile devices. We would like to open a small, clean page showing ONLY the ['content'] of that particular article. I'm looking for a way to link to a page showing only this content - no header, no footer, nothing.
I know that I could create a new page to handle all of this, but it needs to be dynamic. New articles are created regularly, and I'd like to avoid having to add another page to handle this for every article, while also making it simple for the writing team to integrate this feature.
One of my ideas so far is:
Pass a GET parameter to the URL "link" in the JSON - something like - www.mysite.com/article1?contentOnly=true. Then, in my article, detect this parameter in PHP and handle accordingly. I would need this snippet on each article written, so it may cause issues down the road if our staff writers forget to add it.
I haven't worked with ModX long, so I'm assuming there's a better way to handle this. Any ideas would be greatly appreciated. Please let me know if I need to provide more information.
I am not 100 % sure how you have done this, but here's my tip.
Don't use the resource itself to output the JSON. Doing this based on a GET-paramter will required the entire site to be uncached. Instead, use a single resource for the feed and supply the id/permalink there.
For example: mysite.com/feed?id=1, mysite.com/feed?latest or something like that.
Done this way, you could have an empty template with just the snippet that is parsing to JSON in it. This has to be uncached of course, but the rest of the site could be cached as normal.

Facebook-WordPress comment/feedback integration

Currently I have my Facebook profile automatically republish blog posts from a WordPress instance.
What I would like to be able to do, however, is to also have comments posted to either the blog of Facebook show up on the other in the appropriate location.
Is there a way to do this with the Facebook API?
There's a bunch of facebook-connect enabled WordPress plugins that might help you achieve this.
Here's a couple:
Sociable Facebook Connect
WP-Facebook-Connect
yoavf answer is good. A list including those plugins is maintained in Facebook's wiki:
http://wiki.developers.facebook.com/index.php/Facebook_Connect_Plugin_Directory
http://dentedreality.com.au/2008/12/implementing-facebook-connect-on-wordpress-in-reality has a novel approach that isn't on that list.
you need to go to the source of the blog, figure out how the comment form is posted (POST or GET) then you can use mock ajax documentation here. then inside the facebook app, you build a form similar to the one for the blog comment. make sure all the ids of the respective form elements are identical. then on your submit button you have a link like this.
<a clickrewriteid="dummy" clickrewriteform="comment_form"
clickrewriteurl="blog_form_action">Submit</a>
so dummy is the id of an element that will recieve any output.
comment_form is the id of the form that will be posted to the blog comment processor.
blog_form_action is the url in your blogs form action attribute.
if things work, the form comment_form will be posted to your blogs processor page so that the comment submitted from facebook will appear on the blog as well.
if the blog uses GET as its method, you just make sure that blog_form_action contains the query string
hope that helps!

Categories