Different WP Rest API custom endpoints across different themes

I have created a custom theme on WordPress with custom WP Rest API endpoints. This is how I have set it up.

Theme name: Theme 1

Added below line in functions.php

require get_parent_theme_file_path( ‘/inc/myapi-functions.php’ );

And myapi-functions.php consists all the custom APIs. Let’s take an example endpoint

add_action(‘rest_api_init’,’lmsRoutes’);

function lmsRoutes(){
register_rest_route( ‘abcAPIRoute/v1’, ‘login’, array(
‘methods’ => ‘POST’,
‘callback’ => ‘loginABCuser’
));
}

And the loginABCuser is as below

function loginBUuser($request){
//do some wp_rest_get which returns a response ‘my dummy text 1’
$response = // wp_rest_get response;
return $response;
}

The above work perfectly well. I get the response my dummy text 1 as required.

Now the url in wp_rest_get responses keeps changing every time some change happens on the remote API logics. However it is backward compatible in version. Example, I can keep sending the data a v1 url and get the v1 response even if the remote API is in v5 version.

I initially thought using cPanel of the hosting provider I can clone the current theme, make changes in the myapi-functions.php and do the necessary testing and development in the cloned theme, and then swap the live theme to cloned theme. Example would be as below (for loginABCuser in cloned theme)

function loginBUuser($request){
//do some wp_rest_get which returns a response ‘my dummy text 2’
$response = // wp_rest_get response;
return $response;
}

I expected the response to be my dummy text 2 but it still returns my dummy text 1

How do I handle this and where am I going wrong?

I even tried changing

require get_parent_theme_file_path( ‘/inc/myapi-functions.php’ );

to

require get_theme_file_path( ‘/inc/myapi-functions.php’ );

in functions.php but still the same issue.

I can always make the changes and test them in a local environment, but I was wondering if I am limited to do it using cPanel file editor and WP Theme preview, how do I go about it?

And what are the best practices to handle such changes in WP Rest API custom endpoints?

Read more here:: Different WP Rest API custom endpoints across different themes

Leave a Reply

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