BuddyDev

Making BuddyPress Activity Searchable

If you are looking for a simple way to make activity searchable, You can just install this plugin from here https://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.

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

  • Insert the Activity in the search drop down box
  • Filter the query string 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

and here goes our utility functions inside the class

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.

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.

so, here is our final code for the class

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 https://buddydev.com/plugins/bp-searchable-activity/ 

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 😀 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!

    • Thanks for the update Brajesh as well as the javascript tip!

    • Hi Jonatas,
      Sorry for the delayed reply. No, The BuddyPress does not allow this at the moment.

  • 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!

    • Hi John,
      Please download the plugin from here

      https://buddydev.com/plugins/bp-searchable-activity/

      It will take care of everything. You don' t need to manually inject the code if you use this plugin.

      • thanks a bunch Brajesh!

      • For some reason it's not meshing well with my site, it is a customized site with bp social plugin, etc.. It shows up on search button/tab, but when i search under activity, it messes up my social tabs, which includes activity, profile, messages, friends, groups, media, classified…. weird…

        • Hi John,
          Can you please link me to the site(If it is private, please drop me a message).

          I will check and let you know the issue.

          Thanks
          Brajesh

      • My site it private for now so please send me a message back to my email at johnkim1970uw@hotmail.com. I'll then send you how to access the site. Thanks a bunch Brajesh!!

      • My site it private for now so please send me a message back to my email at johnkim1970uw@hotmail.com. I'll then send you how to access the site. Thanks a bunch Brajesh!!

        • Had sent you a mail. Have yet to recieve any message. Please do link me if you want me to take a look.

  • 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!

    • Hi John,
      I am sorry for the delayed reply.
      You are right about the bug. Just tested it. Looking at the code for fix. will have an update soon 🙂

    • Found the bug. That was a side effect of not loading the js file on other page. Pushing an update in a minute.

    • Hi John,
      I have updated the plugin and linked at the top & bottom of this page. Please upgrade to version 1.0.2. It fixes the issue.

      Thanks
      Brajesh

      • thanks, Brajesh! Works fine – confirmed.