Alena
Alena

Reputation: 1214

WordPress: Customize Ajax request response via filter

There is a function in parent theme and I want to customize success message, I don't want to make change in parent theme file. And the function is not pluggable so I can't override it. After digging, I found that I can use add_filter hook to filter response.

I wrote this code:

add_filter( 'wp_ajax_sync-data', 'custom_sync_data' );

function custom_sync_data(){
   $response = array(
               'success' => true,
               'message' => 'Date is updated'
               );
   wp_send_json($response);
}

It does the job but it always returns success message, without any validation. I don't know, how to pass parameter for validation or decision making.

Upvotes: 1

Views: 597

Answers (1)

zipkundan
zipkundan

Reputation: 1774

Can you try this

function custom_sync_data($params){
    //$params can be data or parameters you will have to pass
    //or you will have to check
    //based on that your setup validation or logic
    $response = array(
        'success' => true,
        'message' => 'Date is updated'
    );
}
wp_send_json($response);

You may also use var_dump($params) to check if anything is passed to function.

Hope this helps.

Upvotes: 1

Related Questions