$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Different WP Rest API custom endpoints across different themes|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Different WP Rest API custom endpoints across different themes

matteradmin8PV0评论

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?

Post a comment

comment list (0)

  1. No comments so far