Modifying WordPress API Base Endpoint URL Prefix for WP REST API

January 27, 2020 0 comments
default placeholder

If you’ve spent any time in the WordPress community,  you might have heard about made to the new REST API. However, unless you’re an experienced developer, you may not have any idea what the WordPress REST API actually is.

While the technical details are a bit complex, the basic concepts behind this feature are easy enough to grasp. The new API helps expand what WordPress as a platform can do.

REST APIs have a base URL to which the endpoint paths are appended. The base URL is defined by schemeshost and base Path on the root level of the API specification.

The WordPress developers Community developed its REST API more simpler than ever for developers to connect WordPress with other sites and applications.

This post has been updated for the current WordPress REST API. The new URL for the endpoints contain the prefix as well as a namespace.  Here is an example endpoint,

https://ashokkuikel.com/wp-json/wp/v2/posts/1816

That will fetch the details of first post. The the above example, “wp-json” is the rest url prefix while the “wp/v2″ is the namespace.

If you don’t like the default base prefix and want to change it from “wp-json” to something more beautiful, the code below will help you. In the code below, I have changed the base prefix from “wp-json” to “custom_endpoint“.

add_filter( 'rest_url_prefix', 'modify_endpoint_api_slug');
function modify_endpoint_api_slug( $slug ) { return 'custom_endpoint'; }
flush_rewrite_rules(true);

Once you put the above code in your theme’s functions.php or in a plugin, Please visit the Dashboard->Settings->Change your permalinks( You can restore it after saving again, we need to change it at least once to flush the rewrite rules ).

As soon as you are done with flushing the rewrite rules, you can access all the data from your new custom_endpoint like this.

https://ashokkuikel.com/custom_endpoint/wp/v2/posts/1816

Now, that is not the nicest looking url, but the version 2 of the API has made it necessary to have namespaces in the prefix(wp/v2 is the namespace here).

Want to learn about  WP REST API ? You can read the handbook here.

https://ashokkuikel.com/how-to-redirect-http-to-https-automatically/

Ashok kuikel

Ashok Kuikel is DevOps Engineer(Cloud Computing and Cyber Security), Entrepreneur working on Socio-Economic Development via Technology

He has been actively contributing as Joint Secretary of Federation of Computer Association of Nepal Kavre Chapter. Beside that, he is an official Global Peace Ambassador for Global Peace Chain, Nepal Chapter and Member of Internet Society, Nepal Chapter.

Above all, he enjoys learning about new trends and technologies and loves to share innovative ideas to contribute for the growth of the industry.

You can follow me on Social Media, GitHub, and via my Blog Channels.

Leave a Reply

Articles and Tutorials

We love writing about WordPress and latest plugins tutorials, WooCommerce stats, and much more.