• Alexey

    (@kutuzoff)


    I want to edit the output of search method of post tags.
    I found that using function rest_prepare_post_tag is possible.
    My code is

    function rest_prepare_post_tag( $response, $object ) {
            $response->data['post_tag'] = 'test value';
        return $response;
    }
    add_filter( 'rest_prepare_post_tag', 'rest_prepare_post_tag', 10, 2 );
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    Your callback is passed a WP_REST_Response object. It’s best to use class methods to alter the data contained in the object, but you can directly manipulate some properties. If you do this, var_dump() (see below) the object so you can see the data structure employed. You need to maintain the same sort of data structure. This will also tell you how to correctly access any particular property you want to change.

    Some of the properties could be protected. Such properties can only be altered through declared public class methods.

    Re: var_dump() — this function is not very useful when the browser is not expecting normal output. The function’s output tends to disappear. You may need to pass the output to a log file in order to see the data. I know one dev who likes to email himself such output by passing the return from print_r($responsse, true) to wp_mail().

    Thread Starter Alexey

    (@kutuzoff)

    I know what is doing vardump. The problem is that when I tried, on Ajax page I see same result.
    I tried by return var dump of data

    Thread Starter Alexey

    (@kutuzoff)

    Can I see the result on wordpress debug log file?

    Moderator bcworkz

    (@bcworkz)

    Not without writing code to write to that file. You could try error_log( print_r( $response, true ));, which goes to PHP’s error log. Location is variable, dependent upon host. error_log() doesn’t handle multi-line output very well. Maybe email isn’t such a bad idea.
    wp_mail('[email protected]', 'Debug Output', print_r( $response, true ));

    (the print_r() code in my previous message has an extra ‘s’ in $response)

    Thread Starter Alexey

    (@kutuzoff)

    No , is not working
    I tried to edit my code as this:

    function rest_prepare_post_tag( $response, $object ) {
    error_log( print_r( $response, true ));
        return $response;
    }
    add_filter( 'rest_prepare_post_tag', 'rest_prepare_post_tag', 10, 2 );

    I suppose that the result from ajax-tag-search , is not passed via rest_prepare

    Moderator bcworkz

    (@bcworkz)

    Apparently not. error_log() is a reliable way to check if a filter is applied or not. If the search is using WP Ajax, the POSTed data will include an “action” value. You can check the data passed to admin-ajax.php with your browser’s network developer tool. The action hook used to reach the PHP Ajax search handler will be constructed as "wp_ajax_nopriv_{$_REQUEST['action']}".

    Next, on a template file, var_dump the global $wp_filter array and search for the action hook used. The callback function used for this action will be listed. Find that function’s source code and decide how you can modify the returned value. hopefully there is a filter you can hook. If not, you may need to override the callback with your own version.

    Thread Starter Alexey

    (@kutuzoff)

    The default wp search is using Get method and not Post. I thought to edit wordpress core files but I really want to solve without rewrite functions.
    So rest prepare its not a solution in this case

    Moderator bcworkz

    (@bcworkz)

    My suggestion applies to GET as well. The action parameter is then an URL query arg, for example example.com/wp-admin/admin-ajax.php?s=search-value&action=my-ajax

    If there is no action parameter, then WP Ajax is not being used and the request is not going to admin-ajax.php. In this case, you need to examine the source code of where ever the Ajax request is going.

    Never edit core code! It will not go well if you do. You need to get to the source code of whatever is handling the Ajax request in order to see what your options are. A last resort option is to create your own Ajax handler based on the original and override the jQuery Ajax call to go to your handler instead.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to format response from ajax tag search?’ is closed to new replies.