BuddyDev

Search

Making BuddyPress Activity Searchable

activity-search-dd

If you are looking for a simple way to make activity searchable, You can just install this plugin from here http://buddydev.com/plugins/bp-searchable-activity/ 

Today, I am going to post about a small experiment I did to make activity searchable by the user.

By default, BuddyPress allows you to search the posts, groups, members, forums etc but not the activitty. Though, the search functionality is built into the BuddyPress, we just need some small tweaks to make it available to the users of our site.

For better code management, I will write a singleton class to handle all the work. Let us call our class BPDevSearchableActivityHelper.

Here is a simple pattern for writing singleton class for those who haven’t heard about it.

class BPDevSearchableActivityHelper{
     private static $instance;//instance of the class itself
     private function __construct() {
             //here goes the initialization code etc
           }
//this is used to access the singleton object

public static function get_instance(){
    if(!isset (self::$instance))
        self::$instance=new self();
    return self::$instance;
}

For our purpose, we will need to do the following things

  • Insert the Activity in the search drop down box
  • Filter the querystring to account for the activity search term
  • Redirect to activity directory for showing the final result

I will put all the add_action/add_filter in the private constructor for all these utility functions.

so, our constructor looks like this


private function __construct() {

add_filter('bp_search_form_type_select_options', array($this,'include_activity_in_seach_option') );//injects the Activity in the drop down box

add_filter('bp_core_search_site',array($this,'search_activity_url'),10,2);//filter search redirect url to point to activity directory

add_filter('bp_dtheme_ajax_querystring',array($this,'add_activity_search_to_query'),10,7);//tweak query string to account for the search term.

}

and here goes our utility functions inside the class


/*Inject Activity in the Search drop down*/

function include_activity_in_seach_option($options) {

if(bp_is_active('activity'))

$options['activity']=__('Activity');

return $options;

}

That’s all we need to tell BuddyPress and BuddyPress will insert Activity  into the search dropdown box like this

Now, when the form is submitted, we will need to filter the search redirect url.


function search_activity_url($url,$search_terms){

$search_which = $_POST['search-which'];//what is being searched?

if($search_which!='activity')//is it activity? if not, let us return

return $url;

$slug=bp_get_activity_root_slug();

$url =home_url( $slug . '/?s='. urlencode( $search_terms ));

return $url;

}

As you may see, we check if the person is looking for activity and if that is right, we return the appropriate activity directory url appended with the search term.

Now, for the final step, we will need to filter the bp_ajax_querystring. To avoid the pain of parsing a lot, I have instead filtered over the bp_dtheme_ajax_querystring and It may be too much theme dependent.

Here the utility function that filters on it.


function add_activity_search_to_query($qs, $object, $object_filter, $object_scope, $object_page, $object_search_terms, $object_extras){

if($object!='activity')//do not filter if the current component is not activity

return $qs;

if(empty($qs)&&!empty($_REQUEST['s']))

$qs="search_terms=".$_REQUEST['s'];

return $qs;

}

so, here is our final code for the class


/**

* A class to include all the functionality to allow activity search

*/

class BPDevSearchableActivityHelper{

private static $instance;

private function __construct() {

add_filter('bp_search_form_type_select_options', array($this,'include_activity_in_seach_option') );

add_filter('bp_core_search_site',array($this,'search_activity_url'),10,2);

add_filter('bp_dtheme_ajax_querystring',array($this,'add_activity_search_to_query'),10,7);

}

public static function get_instance(){

if(!isset (self::$instance))

self::$instance=new self();

return self::$instance;

}

/*Inject Activity in the Search drop down*/

function include_activity_in_seach_option($options) {

if(bp_is_active('activity'))

$options['activity']=__('Activity');

return $options;

}

function search_activity_url($url,$search_terms){

$search_which = $_POST['search-which'];//what is being searched?

if($search_which!='activity')//is it activity? if not, let us return

return $url;

$slug=bp_get_activity_root_slug();

$url =home_url( $slug . '/?s='. urlencode( $search_terms ));

return $url;

}

function add_activity_search_to_query($qs, $object, $object_filter, $object_scope, $object_page, $object_search_terms, $object_extras){

if($object!='activity')

return $qs;

if(empty($qs)&&!empty($_REQUEST['s']))

$qs="search_terms=".$_REQUEST['s'];

return $qs;

}

}
//Instantiate the singleton helper object
BPDevSearchableActivityHelper::get_instance();

That makes our activity search complete.

Here is a screenshot from my test install when I searched for test

Now, there is one small problem we may face. On the search page, if you click load more, It won’t honor our search term. To make that happen, we will need to pass the search term via javascript.

I could not find a way without modifying the bp-default’s global.js. Here are the lines I modified/adedd as a patch file.

Patch File :https://gist.github.com/2933986.js

and you may find the completely modified global.js here(It is from BuddyPress v 1.5.6)

http://pastebin.com/65i9GSTH

I hope that you will like to implement it on your sites.  Please do let me know your experience or the suggestion for improvements in the comments.

 

If you are looking for a simple way to make activity searchable, You can just install this plugin from here http://buddydev.com/plugins/bp-searchable-activity/ 

Get latest WordPress & BuddyPress tutorials, news and tricks in your inbox.

We send out a lovely email newsletter with the most popular stories. Be a part of it.

42 Responses to Making BuddyPress Activity Searchable

  • Hi Brajesh,

    Great work !!
    I think you can achieve your goal without modifying bp-default global.js file using cookies as BuddyPress send the cookies in an ajax var.

    First, we’ll need to enqueue a new javascript file, let’s say src-activity.js :


    // this is src-activity.js to enqueue
    jQuery(document).ready(function($){

    if( $('form#search-form #search-which').length ) {

    if ( '' != $.cookie('bp-activity-search-terms') ) {

    $('form#search-form #search-which option[value="activity"]').prop( 'selected', true );
    $('form#search-form #search-terms').val( $.cookie('bp-activity-search-terms') );
    }

    $('#search-submit').click(function(){

    if( $('form#search-form #search-which').val() == "activity" ) {

    search_terms = $('form#search-form #search-terms').val();

    if(search_terms.length == 0)
    $.cookie( 'bp-activity-search-terms', '', {path: '/'} );

    else
    $.cookie( 'bp-activity-search-terms', search_terms, {path: '/'} );

    } else {
    $.cookie( 'bp-activity-search-terms', '', {path: '/'} );
    }

    })

    }

    });

    Then in your the final code of your class, i need to remove line 65 and 67 and replace it with this code :

    $_BP_COOKIE = &$_COOKIE;
    if ( isset( $_BP_COOKIE['bp-activity-search-terms'] ) && !empty( $_BP_COOKIE['bp-activity-search-terms'] ) ) {

    if( empty($qs) )
    $qs="search_terms=".$_BP_COOKIE['bp-activity-search-terms'];

    else
    $qs .= "&search_terms=".$_BP_COOKIE['bp-activity-search-terms'];

    }

    Then when you click load more > you still have the search_terms in the querystring, and it’s still there on all scopes ;)

    Just in case, the code is not readable, here’s the pastebin of it http://pastebin.com/SGvRmqVe

    What do you think of this alternative ?

    • Thank You imath.
      Your code works wonders. It’s a great addition as it removes the dependence on editing core js file :)

      I just modified one line to deselect activity in the drop down if the user goes on to another page by clicking links.

      Here is it as a plugin
      https://github.com/sbrajesh/bp-searchable-activity

      Regards
      Brajesh

      • Good Evening Brajesh,

        Great plugin. And thanks for the line 12 or your main plugin file :)

      • can this code work for other pages like ‘classified’? also making that page searchable or other pages we might have created, using the same codes and just replacing ‘activity’ part with ‘classified’ for example?

        • Hi John,
          It should work for classified.
          You will need to ask the developer of classified if they have some custom loop and what paramaters are required to pass for the searching. Then, you can model it after activity or blog search.

  • Oh, this is total awesomeness from you both :D I’ll try it in my install! Thanks a lot

  • Thank you Brajesh and iMath! Slick!

  • sbrajesh and imath, great work gentlemen!
    cheers :-)

  • Hello @sBrajesh,

    Looking at the page source, it appears that the plugin is running the searchable.js script sitewide? I see the script listed on the plugins Network Admin age source. Could you resolve? Thanks!

  • can this work for other pages like “classified” searchable?

    • Hi John,
      you can make it work with any plugin/component/post_type.
      I assume that classifieds is a post type, so you will just need to mod the search-loop.php a little and pass the post type with search term(or may be use WP_Query/query_posts) and It should work.
      Sorry for the delayed reply.

      • Hi Brajesh, thanks for the reply. Btw, I’m very new to wp & bp and not a programmer myself but trying to build something for my new idea, a social hybrid site, for just proof of concept w/o spending thousands of dollars, so every tips, hints and ideas from guys like yourself are much appreciated so thanks again. So anyway, is it possible then I could just replace the word ‘activity’ from your codes to ‘classified’ to make it work for my site? and do i just copy and paste into site specific plugin or theme’s functions.php file? Thx!!

  • Hi Brajesh & imath,

    This is a great plugin, you both do great work!

    I think I may have found a minor bug in the searchable.js that I wanted to pass along.

    I am seeing that the $.cookie(‘bp-activity-search-terms’) is actually null when the cookie isn’t set. So the cookie check right under the “//do we have some cookie set?” comment is returning a false true when the cookie isn’t set.

    The issue comes in if there are no search options selected and the Activity option isn’t the first in the search list. In that situation it is still selecting the Activity option even though it shouldn’t be.

    I updated the following line:

    if ( ” != $.cookie(‘bp-activity-search-terms’) ) {

    To be:

    if ( $.cookie(‘bp-activity-search-terms’) != null && ” != $.cookie(‘bp-activity-search-terms’) ) {

    Which seemed to fix the issue.

    Thanks again for the great plugin!

    • Hi Matt,
      thank you for the comment and the details of the issue.
      Since javascript treats null as well as empty strings as boolean value, we can just do away with the test like this


      if ( $.cookie('bp-activity-search-terms') )

      Just pushed an update on github :)

    • So where do I inject this code to or do I have to create a new post or page or at search.php

  • Hello! Thanks for the plugin! It worked very fine!
    Great work!
    I would like to search in activity stream by the author of the post.
    Is that possible?

    Thank you again!

  • Sorry guys, i’m new to using wp/bp so where do i inject this code into, do i need to make a new search page. Would someone please help me with step by step… Please… Great work by Brajesh, Imath, M Ryken!

  • nice info.thanks

  • Thanks for the great plugin Brajesh. It works perfectly well in the main activity page and public group activities, however it does not return any results in hidden group activity pages. How can I make this plugin return search results in hidden group activities?

    • Hi Sezen,
      Thank you for joining BuddyDev and supporting us.
      I am sorry but I am not sure about the question. Do you mean including the activities from private/hidden groups in the sitewide activity page?

  • Hi Brajesh,

    I mean “being able to search activities in hidden/private group page” . Currently this search does not return anything if the group the user in is hidden/private. It is fine if the group is public.

    Thanks.

    • Hi Sezen,
      I am sorry for the trouble. Can you please provide a screenshot and tell me a little more about your BuddyPress/WordPress version?

  • I have BP 1.8.1 and Wp 3.6. You can login at 1sttalk.com via your FB account, and create a hidden group and try searching there.

  • Hi, any update on “not being able to search activity in hidden groups”?
    Thanks.

    • Hi Sezen,
      I am sorry I could not reply. Was down with high bp for couple days.
      I had registered on your site and tested the thing.

      Do you mean the drop down in the right sidebar as I could not see much. I don’t see activity at all in that drop down. Can you please mail me the details on brajesh[at] cosmiccoders.com and I will look into it quickly.

      Thanks
      Brajesh

  • hi! Maybe I’m wrong, but it looks like there is a small issue.

    If in activity directory we search for some activity term, move to other page and submit activity term again, we will be taken back to the activity directory, but cookie (term) won’t be reset. Basically, we will get results for the old term.

    Please, let me know if it’s some issue on my side, thanks!

Leave a Reply

Are you looking for support?

Please join BuddyDev Premium Club to get timely support. At the moment, We are unable to provide much support in comments. By Joining BuddyDev Premium Club, you will get guaranteed support(and we have 30 days money back guarantee too).

Your email address will not be published. All the fields marked as * are required.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>