最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to do admin ajax request in a plugin for rest api

programmeradmin4浏览0评论

This must be something very simple, but I cannot figure it out.

I'm working on a plugin, that extends a theme's functionality to rest api. Theme has an ajax action function that updates user profile. Here's the code:

add_action( 'wp_ajax_nopriv_update_profile', 'update_profile' );
add_action( 'wp_ajax_update_profile', 'update_profile' );

if( !function_exists('update_profile') ):
    function update_profile(){
        //update profile discipline
        ......    
        echo json_encode( array( 'success' => true) );
        die();
    }
endif;

Now I want to use this action in my plugin, and do something after it has returned. But since it has die() at the end, I cannot continue my execution after action. Here's how I'm calling it in my plugin:

add_action( 'rest_api_init', function () {
    register_rest_route( 'mobile-api/v1', '/update-profile', array(
      'methods' => 'POST',
      'callback' => 'editProfile',
    ));
});
function editProfile() {
    do_action("wp_ajax_update_profile");

    $response = array(); //<---- this doesn't get executed
    $response["works"] = "Voila!";
    wp_send_json($response, 200);
}

I've tried ob_get_contents() but it won't go past the die method.

Editing the theme is not an option.

How can I avoid the die() from theme action and return my own response?

Or what is the best way to do a an admin ajax request from plugin.

发布评论

评论列表(0)

  1. 暂无评论