Custom WordPress Rest API Plugin

Have you been playing around and using the WordPress API? What do you think of this powerful tool?

The WordPress REST API gives us API endpoints to pull data types 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 is a great tool. Whether your using it for fun or to building headless application using WordPress as the back-end and the Angular or other JavaScript frameworks for the front-end this plugin can simplify the workflow..

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 looking through the data it can be challenging to find the simplest thing at times. So here I’m showing you what I’ve created a Custom WordPress plugin that simplifies this call back and will only return the JSON that I specify (not the whole API call).

What’s JSON is necessary you may ask? Let me explain below.

First you would need to download and install the plugin from Custom WP API Plugin

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!

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 would call your list of products –

YOUR_DOMAIN/wp-json/api/v1/product

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

Leave a Comment