During our Quarterly Q&A Meetup a question was asked on how a site could be setup using Gravity Forms form to collect posts that then have those posts become available on a different WordPress installation.
For example, students could write an article about a topic that then publishes on site specific to that topic. There maybe other ways to solve this problem, but for the sake of this article, here’s how you can use Gravity Forms to collect posts, then have those articles published on separate WordPress sites.
IMPORTANT: This is technical and requires the ability to write PHP for WordPress.
To start, you’ll need the Gravity Forms plugin (full disclosure, it is a paid plugin). You can download it here: http://www.gravityforms.com
Next you’ll want to create a new Gravity Form form using the Post Fields. See screenshot below:
When setting this form up, let’s configure the new post options. Expand the Post Title and make sure the Post Status is Draft.
Let’s publish a page on our site with this form.
Now here’s the form where users can submit their posts. Don’t worry, these posts won’t go live. If you recall, we configured the form to only set the status of each post as a draft.
Now that we have the form to collect the posts, let’s use the WordPress REST API to grab the posts on a different WordPress site.
These next steps will be done on the third-party site reading the information from the site collecting the data above.
To do this, it will require some development. In short, here’s what you need to do on the third-party website to import these posts using the REST API.
Create a wp_cron() job to look for new posts to import on a periodic basis.
When the cron job executes you’ll want to fetch the posts using wp_remote_request() with authentication. With authentication means you pass along headers on the request. Use this template below. Replace YOUR_USERNAME and YOUR_PASSWORD with the credentials of the site where you’re collecting the posts (the site using the Gravity Forms we setup above). You will not be able to retrieve posts that are set as “draft” without authentication.
$args = array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( YOUR_USERNAME . ':' . YOUR_PASSWORD ) ) ); $post_results = wp_remote_request( $url, $args );
For the URL you’ll want to query posts by specific category and status. Here’s an example:
The request will return the post data. Iterate over the results and use wp_insert_post() to add this post into the new site.
Couple things to note:
- I choose to have the site that collects the posts set the post status as “draft” so that these posts are not public (or ever public on that site).
- You may want to use the WP REST API to update the status or delete the post on the site where it was submitted. You can learn more about modifying content using the REST API here: https://developer.wordpress.org/rest-api/
- My suggestion would be to write a plugin that can be installed on third party sites that would be configurable (such as category) to import these posts.