Site icon Hip-Hop Website Design and Development

How to use action response in a plugin for rest api

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();
    $response["works"] = "Voila!";
    wp_send_json($response, 200);
}

How can I avoid the die() from theme action and return my own response? I’ve tried ob_get_contents() but it won’t go past the die method. Editing the theme is not an option.