The WordPress REST API gives us endpoints to pull data types from our content management system (CMS) that allow us to work with websites remotely by sending and receiving JSON (JavaScript Object Notation) objects.

I have been working with the WordPress API now for some time and it’s a great way of turning a WordPress website into a back-end CMS for your JavaScript front-end, like Angular, React or Vue. Whether your using it for fun or to building headless application WordPress API as the back-end can get you going quickly and efficiently.

Simplify the WordPress API call back

When using the API, WordPress returns a lot of data in JSON format (we know that right), but this can confuse a new developer when they see it for the first time. When sifting through all that data it can be challenging to find the simplest thing at times. An image for example, I have created a WordPress plugin to simplify the call back to only return the relevant endpoints (data), making it much easy to read (and not the whole API call) into any front-end application.

What endpoints is deemed necessary you may ask? Let me explain below.

Here is a list of what the plugin will do:

  • Return a list of posts
  • Return a list pages
  • Return a list products

Nothing new right? Well only what it returns, way less bloat so to speak. This plugin will only return the post ID, title, name, content, slug and featured images (small, medium, large) that’s it!

Here is how you would use the plugin in your WordPress website.

This is how you would call for the list of all your posts –

YOUR_DOMAIN/wp-json/api/v1/posts

This is how you would call a single post by slug-name / Hello World –

YOUR_DOMAIN/wp-json/api/v1/posts/hello-world 

This is how you would call your list of pages –

YOUR_DOMAIN/wp-json/api/v1/pages

This is how you JSON objects would return the ID, title, content, slug and featured images –


{
 
    "id": 32,
    "title": "Link 2",
    "content": "The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.",
    "slug": "link-2",
    "featured_image": {
        "thumbnail": "http://mysiteName/wp-content/uploads/2019/01/2-150x150.jpeg",
        "medium": "http://localhost/mysiteName/wp-content/uploads/2019/01/2-300x200.jpeg",
        "large": "http://localhostmysiteName/wp-content/uploads/2019/01/2-1024x683.jpeg"
    }
 
},
{
 
    "id": 30,
    "title": "Link 1",
    "content": "Cicero are also reproduced in their exact original form, accompanied by English versions.",
    "slug": "link-1",
    "featured_image": {
        "thumbnail": "http://localhost/mysiteName/wp-content/uploads/2019/01/3-150x150.jpeg",
        "medium": "http://localhost/mysiteName/wp-content/uploads/2019/01/3-300x200.jpeg",
        "large": "http://localhost/mysiteName/wp-content/uploads/2019/01/3-1024x683.jpeg"
    }
 
},

You see how it is a much clean response when only  the ID, title, content, slug and featured images are returned.

This is a much cleaner way off accessing your data to inject in your application. Hope this plugin helps with you  work with the WordPress API more efficiently.

Try this FREE WordPress Plugin to help ease your API endpoints. Custom WP API Plugin

Have you been experimenting with the WordPress API? and if so, what are your used cases? Leave your comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get a free proposal for your business