The WordPress REST API is a powerful tool for programmers to generate content dynamically using post and page API POST calls. You can populate the standard fields such as the title, the permalink (slug), category, content of the page and more using the standard API calls. This is great for standard web pages, but has one main limitation: support for plugins. In this post, we’ll show you how to generate a wordpress post with plugin fields configured using the WordPress REST API.
Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
- Install the Pericror REST API Meta Support plugin. This is needed to update the post meta fields, which plugins read from to get their settings.
- Create a test Post from the WordPress Dashboard in which you have the plugins you want to configure.Configure the plugins with the settings you want.
- Using a browser with developer tools (I will be using Chrome), open the Network traffic view:
- Open Developer Tools (Ctrl+Shift+I)
- Click on the ‘Network’ tab, check ‘Preserve Log’, and update the Filter to ‘post’
- Keep this window open
- Click the ‘Save Draft’ button from the WordPress Publish box. This will send all of the data from your browser to your WordPress site, and allow you to see which fields are being set for the plugins!
- Returning to your Network tab, click on the first “post.php” entry.
- Scroll down the ‘Headers’ section until your reach ‘Form Data’. This contains all of the field names your plugin may have set associated with the post you will need to pass into the ‘meta’ field of your REST API data! Scroll through the field names to find the fields associated with your plugin.
- If I was looking for the Widget After Content and Yoast SEO fields, I would find them here:
- If a field appears to have no corresponding value (i.e ‘field:’ instead of ‘field:value’), it means the default values of the field are set and you don’t need to configure this field.
- When populating the ‘meta’ section of your WordPress REST API page or post POST data, simply add the key value pairs you found above to configure the plugin fields from the REST API!