Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Add Custom REST API Route WordPress

    This blog describes about – how we can add custom REST API routes in WordPress. The use of API is very common now a days. So, when we work with API’s we need some custom routes also with default ones so that we can facilitate intended tasks.

    When we add our custom routes, its important that we don’t affect the default one’s. We should add new routes in with default one’s not in place of default routes. Here we go,

    We use rest_api_init hook which fires when preparing to serve an API request.

    Now, we register our route in callback function of above action using function register_rest_route –

    Here, first parameter represents namespace which should unique to plugin, second is the base url for our route. Third parameter is the argument array. We can use fourth parameter also which is optional i.e override, means if route already exists the route will be override if set to true.

    In methods argument we write GET/POST, here we can also use WP_REST_Server constants, e.g  WP_REST_Server::CREATABLE means PUT/POST,  WP_REST_Server::READABLE means GET, etc.

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .

    Using the callback function we specified while registering the route we can perform actions we want to do using registered route.



    Still have any issue feel free to add a ticket and let us know your views to make the code better

    Thanks for Your Time! Have a Good Day!


    . . .


    Add Your Comment

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again

    Table of Content

    Hide Index