How to create a post with Gravity Forms, then use the REST API to pull those posts into a third party site

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:screen-shot-2017-02-14-at-1-59-50-pm

When setting this form up, let’s configure the new post options.  Expand the Post Title and make sure the Post Status is Draft.

Screen Shot 2017-02-14 at 2.03.53 PM.png

Let’s publish a page on our site with this form.

screen-shot-2017-02-14-at-2-05-44-pm

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.

screen-shot-2017-02-14-at-2-05-58-pm

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:

http://YOUR-WEBSITE.COM/wp-json/wp/v2/posts?categories=6&status=draft

The request will return the post data.  Iterate over the results and use wp_insert_post() to add this post into the new site.

That’s it!

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.
Advertisements

Troubleshooting a WordPress Site: Where Is My Sub-Menu?

How our members worked together to resolve an issue on a WordPress website

At our September 2016 meetup, we held a Q & A workshop where our Metro Detroit WordPress members can get answers about their WordPress site from other members. Or they can ask specific questions about WordPress features and functionality.

Because it’s community-focused, the Q & A workshop is one of my favorite meetup formats. It’s where we learn about each other sites and what members do.

Our Q & A workshops are casual, with people moving from table to table to help each other. Or just listen in to hear what other people are doing on their site.

What’s the Issue?

One of our members was having issues with her self-hosted WordPress website, where the sub-menu wasn’t displaying.

She was using the Divi theme on her site, one of the drag-and-drop premium WordPress themes, which has a lot of built-in functionality that’s usually provided through plugins.

Divi recently released version 3.0, which none of our Metro Detroit WordPress members had worked with.

Nevertheless, several people stepped up to the challenge to try to find a solution. Which they did!

Troubleshooting

Knowing others go through similar situations, trying to figure out why some feature isn’t working as expected on their site, we wanted to document the steps.

1. Backup the Site

Before making any changes, backup your site. That means backing up both the files and the database.

You can find lots of free backup plugins in the WordPress repository. Additionally, there are many premium backup solutions.

WP Beginner recently published a review of several WordPress backup plugins.

Luckily our member already had a backup plugin installed on her site, so a backup was quickly completed.

2. Update WordPress

You always want to have the most current version of WordPress installed on your site.

WordPress releases major updates and security updates regularly, it’s important that your site has the latest version to stay secure.

One of our members helped to get the latest WordPress version updated on the site. Still, the problem persisted.

3. Update Themes and Plugins

As with WordPress, you want to make sure you have the latest versions of the themes and plugins installed.

Developers update themes and plugins to incorporate new features from the latest WordPress version, to fix bugs, and to add new plugin functionality.

If you’re having an issue that is theme- or plugin-related, it’s possible the latest version will fix your problem. Why waste time searching for a solution when an update will resolve the problem?

Her site themes and plugins were updated, but the sub-menu still wouldn’t display.

4. Activate the Latest Twenty XXX Theme

This is a tried-and-true solution to many issues.

The latest Twenty XXX theme is the default theme installed with every new WordPress installation (at the time of this post, the latest is the Twenty Sixteen theme).

These themes are used to rule out any issues that might be theme-related.

Review in Other Browsers and Devices

At times, a browser or device might be the culprit.

Checking the site in different browsers or on different devices (tablets, smartphones, etc.) can help narrow down the cause of the issue.

This wasn’t the case for our member’s site.

Unfortunately, the sub-menu wouldn’t display in other browsers, nor in other devices.

Theme Options

Many free and premium themes have Theme Options that control fonts, CSS, menus, and other features in the theme.

Voilà!

That was where the issue was, a custom CSS (Cascading Style Sheet) setting was the reason the sub-menu wasn’t displaying.

The existing CSS code in her site was copied to a text file for safe keeping. The CSS was updated, saved, and the sub-menu displayed.

In Conclusion

Troubleshooting the WordPress theme on our member’s site only took a few steps. We were lucky it wasn’t a more involved issue, with conflicting plugins or an issue with the web host.

If you find yourself needing to troubleshoot your site, always start by:

  1. Backing up the site, both database and files
  2. Updating to the latest WordPress version
  3. Updating to the latest theme and plugins

Shoutout to our members Angela Samuels, Randy Wright, Andy Melichar, and Eric Malcolm for their help in troubleshooting the issue!

Photo credit: Randy Wright