Skip to:
Content
Pages
Categories
Search
Top
Bottom

bp_ajax_querystring filter not runing


  • Fencer04
    Participant

    @fencer04

    I call add_filter in my plugin and the code is never run. Here is the code for how I’m adding the filter:

    add_filter( 'bp_ajax_querystring', 'my_function', 12, 2 );

    I’m debugging using XDebug and when I perform a member search other ‘bp_ajax_querystring’ filters that are part of core are getting called but not mine. When the page initially loads the code I’ve placed to set the filter is being hit.

    Is there a particular timing/action for adding a ‘bp_ajax_querystring’ filter?

Viewing 15 replies - 1 through 15 (of 15 total)

  • shanebp
    Moderator

    @shanebp

    Just guessing, but try changing the filter priority from 12 to 10 or maybe 50, etc.

    http://hookr.io/filters/bp_ajax_querystring/


    Fencer04
    Participant

    @fencer04

    I’ve tried a ton of numbers all the way up to 999. Nothing works.


    shanebp
    Moderator

    @shanebp

    bp_ajax_querystring is… complicated.

    Perhaps there is another approach… What are you trying to filter?


    Fencer04
    Participant

    @fencer04

    I have a plugin that allows user to block themselves from the member page. It is currently working with regular search but not for the AJAX search.


    shanebp
    Moderator

    @shanebp

    AJAX search involves javascript – that is probably the issue.
    There might be a way to accomplish your task via that filter… if you find it, please share it here.

    For blocking aka exclude, I use this action hook: bp_pre_user_query_construct

    For you, try something like:

    function fencer_members_query( $query_array ) {
    
       $exclude_ids = // collect all the member ids that don't want to appear, as an array
    
       if ( ! empty( $exclude_ids ) )
    	$query_array->query_vars['exclude'] = $exclude_ids;
    
    }
    add_action( 'bp_pre_user_query_construct', 'fencer_members_query', 1, 1 );

    Henry Wright
    Moderator

    @henrywright

    Out of interest can we see your my_function() definition?


    Fencer04
    Participant

    @fencer04

    shanebp – I have another function that is using the bp_pre_user_query_construct hook. It is not run when AJAX search is run. In my research this is normal and bp_ajax_querystring is the way to go for these cases.

    Henry Wright – Here is the code and hook exactly how it is in my plugin file:

    function sbpp04_hide_from_ajax_search( $query_string, $object ){
    	if( !empty( $query_string ) ){
    		$query_string .= "&";
    	}
    
    	$query_string .= "exclude=2";
    
    	return apply_filters( 'sbpp04_hide_from_ajax_search', $query_string, $object );
    }
    add_filter( 'bp_ajax_querystring', 'sbpp04_hide_from_ajax_search', 50, 2 );

    shanebp
    Moderator

    @shanebp

    Just guessing…
    Instead of a filter, try using the action hook:

    add_action( 'bp_legacy_theme_ajax_querystring', 'sbpp04_hide_from_ajax_search', 50, 2 );

    https://codex.buddypress.org/developer/function-examples/bp_ajax_querystring/


    shanebp
    Moderator

    @shanebp

    btw – the example function I posted above works to exclude on ajax search too – at least for me.


    Fencer04
    Participant

    @fencer04

    No matter which option, filter or action, I use the code isn’t called when the AJAX search is run. Is there a particular time when I need to add the action or filter? Does it matter that I’m doing this through a plugin?


    Henry Wright
    Moderator

    @henrywright

    Inside your plugin try adding:

    add_action( 'init', function() {
        add_filter( 'bp_ajax_querystring', 'your_function', 10, 2 );
    }

    Of course, also define your function:

    function your_function( $ajax_querystring, $object ) {
        // Code here.
        return $ajax_querystring;
    }

    Fencer04
    Participant

    @fencer04

    Still nothing, I’m using XDebug to see if the function is being called. On the first page load the function gets called. After that I catch the core bp_ajax_querystring is getting called but not my function. It is really frustrating. If anyone knows of a plugin that uses it that I could review that would be helpful also.


    Fencer04
    Participant

    @fencer04

    So I moved my function into the functions.php file in my theme and now it is working with AJAX. It is something about being in my plugin that is causing it run only when the page is initially loaded. Any ideas?


    shanebp
    Moderator

    @shanebp

    Did you try putting it in the plugin loader?
    iow. the file that starts with the plugin meta info.
    Remember that wp ajax requires access to wp-admin


    Fencer04
    Participant

    @fencer04

    That did the trick. Thank you, I have thought about that a ton of times but never tried it. I really appreciate the help.

    Is there anyway to be able to put that function in the other file? It would be nice to keep it with the others.

Viewing 15 replies - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.
Skip to toolbar