BuddyDev

Search

by Brajesh Singh

Permalink: #788

Make user autojoin to the current Group if They register from group page using Bp Ajax registration plugin


class BPAjaxR_Autojoin_Current_Group{ private static $instance; private function __construct() { add_action( 'bp_after_account_details_fields', array( $this, 'add_group_field' ) ); add_action( 'bp_signup_usermeta', array( $this, 'update_signup_meta' ) ); add_action( 'bp_core_activated_user', array( $this, 'join_on_activation' ), 10, 3 ); } /** * Get the singleton instance * @return BPAjaxR_Autojoin_Current_Group */ public static function get_instance(){ if( !isset ( self::$instance ) ) self::$instance = new self(); return self::$instance; } /** * add current group as hidden field on registration page */ public function add_group_field() { if( !bp_is_group() ) return ; echo "<input type='hidden' name='current-group' value='". bp_get_group_id( groups_get_current_group() )."' />"; } /** * Add the group field to signup meta * @param type $meta */ public function update_signup_meta( $meta ) { //check if the group field is set if( $_POST['current-group'] ) $meta['group_to_join'] = (int) $_POST['current-group']; return $meta; } /** * Make user join the group when account is activated * @param type $user_id * @param type $key * @param type $user */ public function join_on_activation( $user_id, $key, $user ) { $meta = $user['meta']; if( empty( $meta['group_to_join'])) return ; $group_id = (int) $meta['group_to_join']; groups_join_group($group_id, $user_id ); } } //initialize BPAjaxR_Autojoin_Current_Group::get_instance();

Please put the above code in bp-custom.php

#bp-hacks, #registration #bp-ajax-registration

by Brajesh Singh

Permalink: #778

Add a User to a perticular group when User account is activated

add_action( 'bp_core_activated_user', 'bpdev_join_group_on_signup');

function bpdev_join_group_on_signup( $user_id ){

    $group_id = 3;//change it to the group id you want the user to join

    groups_join_group( $group_id, $user_id );

}

#buddypress #bp-hacks

by Brajesh Singh

Permalink: #696

Get Poke Count for a User


function bp_poke_get_poke_count( $user_id ){ $pokes = bp_get_user_meta( $user_id, 'pokes', true ); if( ! empty( $pokes ) ) return count( $pokes ); return 0; }

You can pass bp_displayed_user_id() while calling this function to show the poke count of diplayed user. To show the poke count of logged in user, pass get_current_user_id()

#bp-poke #bp-hacks

by Brajesh Singh

Permalink: #679

Fix avatar visibility problem on BuddyPress Multiblog/Multinetwork


function bpdev_fix_avatar_dir_path( $path ){ if ( is_multisite() && BP_ENABLE_MULTIBLOG ) $path = ABSPATH . 'wp-content/uploads/'; return $path; } add_filter( 'bp_core_avatar_upload_path', 'bpdev_fix_avatar_dir_path', 1 ); //fix the upload dir url function bpdev_fix_avatar_dir_url( $url ){ if ( is_multisite() ) $url = network_home_url('/wp-content/uploads') ; return $url; } add_filter( 'bp_core_avatar_url', 'bpdev_fix_avatar_dir_url', 1 );

#bp-hacks #avatar #multinetwork #multisite

by Brajesh Singh

Permalink: #662

Show the faces of users who liked a BuddyPress Activity

if( !function_exists('bp_activity_add_meta')):
function bp_activity_add_meta( $activity_id, $meta_key, $meta_value ) {
    global $wpdb, $bp;

    // Make sure activity_id is valid
    if ( !is_numeric( $activity_id ) )
        return false;

    // Sanitize key
    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );

    // Sanitize value
    if ( is_string( $meta_value ) )
        $meta_value = stripslashes( esc_sql( $meta_value ) );

    // Maybe, just maybe... serialize
    $meta_value = maybe_serialize( $meta_value );


    $wpdb-&gt;query( $wpdb-&gt;prepare( "INSERT INTO {$bp-&gt;activity-&gt;table_name_meta} ( activity_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $activity_id, $meta_key, $meta_value ));


    //should we consider invalidating the cache
    wp_cache_delete( 'bp_activity_meta_' . $activity_id . '_' . $meta_key,  'bp' );

    // Victory is ours!
    return true;
}
endif;
add_action( 'bp_activity_add_user_favorite', 'bpdev_track_user_favorite', 10, 2);

function bpdev_track_user_favorite( $activity_id, $user_id ){

    bp_activity_add_meta( $activity_id, 'favorited_by_user', $user_id );
}
add_action( 'bp_activity_remove_user_favorite', 'bpdev_track_user_unfavorite', 10, 2);

function bpdev_track_user_unfavorite( $activity_id, $user_id ){

    bp_activity_delete_meta( $activity_id, 'favorited_by_user', $user_id );
}

//show faces, yay!

add_action( 'bp_activity_entry_content', 'bpdev_show_who_favorited_activities' );
function bpdev_show_who_favorited_activities(){
    $output = '';
    $favorited_users = bp_activity_get_meta( bp_get_activity_id(), 'favorited_by_user' );
    //print_r($favorited_users);
    if( !empty( $favorited_users ) ){

        foreach( (array) $favorited_users as $user_id )
            $output .= "<a href='" . bp_core_get_user_domain ( $user_id ) . "'>". bp_core_fetch_avatar ( array( 'type'=&gt; 'thumb', 'height'=&gt; 25, 'width'=&gt;25, 'item_id'=&gt; $user_id ) ) . "</a>";

    }
    if( $output )
        echo "<div class='clearfix activity-favorited-by'>{$output}</div>";
}

Put the above code in bp-custom.php

#activity #bp-hacks

by Brajesh Singh

Permalink: #637

Get the total number of favorites accumulated by a user on his activities

/**
 * Get the total no. of favorites accumulated by a user on his activities 
 * @global type $bp
 * @global type $wpdb
 * @param int $user_id
 * @return int count of total favories by other users on given users activity 
 */
function bpdev_count_user_acivity_favs( $user_id ){

    global $bp, $wpdb;
    $query =$wpdb->prepare("SELECT SUM(meta_value) as total FROM {$bp->activity->table_name_meta} WHERE meta_key = %s AND activity_id IN ( SELECT id FROM {$bp->activity->table_name} WHERE user_id = %d)", 'favorite_count', $user_id );


    return (int) $wpdb->get_var( $query );

}

#activity, #bp-hacks

by Brajesh Singh

Permalink: #428

Extending Breadcrumb Trail plugin for BuddyPress breadcrumb navigation

//filter and append our items to breadcrumb trails
add_filter('breadcrumb_trail_items', 'hibuddy_include_bp_items',10,2);

function hibuddy_include_bp_items($trail, $args){


     global $bp;

     //current action
     $action = isset( $bp->canonical_stack['action'] ) ? $bp->canonical_stack['action'] : '' ;
     $component = isset( $bp->canonical_stack['component'] ) ? $bp->canonical_stack['component'] : '';
     $action_variables = isset( $bp->canonical_stack['action_variables'] ) ?  $bp->canonical_stack['action_variables'] : array();

     $trail_end = '';

     //is user component
     if( bp_is_user() ){
         //remove trail_end
        unset($trail['trail_end']);
        //add Members Page as Link
        $trail[] = "<a href='".get_permalink($bp->pages->members->id)."'>".get_the_title($bp->pages->members->id)."</a>"; 


        //if component is empty, it will just list the user display name as end point
        //we may improve it to show the default component title instead in future

        if( empty( $component ) ){
            $trail['trail_end'] = bp_get_displayed_user_fullname ();
        }else{
            //if we are here, we are most probably on a component screen of user
            $trail[] = bp_core_get_userlink(bp_displayed_user_id());
            //find the details about current nav item
            $nav_details = hibuddy_find_nav_details($bp->bp_nav, $component);
            //let us keep the name of current nav menu as end point
            $trail_end = $nav_details['name'];

            //are we doing some action for the component
            if( !empty( $action ) ) {
                //yes, then let us link to the parent component on user
                $trail[] = "<a href='". $nav_details['link']."'>".$nav_details['name']."</a>";

                $subnav_details = hibuddy_find_subnav_details( $component, $action );

                //let us keep that sub nav action name as the end point
                $trail_end = $subnav_details['name'];

            }

            if( !empty( $action_variables ) ){

                //is some action_variable set
                //if yes, let us append the parent action link to the breadcrumb
                $trail[] = "<a href='". $subnav_details['link']."'>".$subnav_details['name']."</a>";

                $trail_end = array_pop( $action_variables );

                foreach( $action_variables as $action_name )
                    $trail[] = ucwords(str_replace('-', ' ', $action_name));

               $trail_end = ucwords( str_replace('-', ' ', $trail_end ) );
            }

        }



    }elseif( bp_is_active('groups') && bp_is_group() ){

         unset($trail['trail_end']);
         //let us append the group parent page as link       
         $trail[] = "<a href='".get_permalink($bp->pages->groups->id)."'>".get_the_title($bp->pages->groups->id)."</a>"; 


        //get the current group details
         $current_group = groups_get_current_group();

         //if no action is set, we are on group home page
         if( empty( $action ) ){
            $trail['trail_end'] = bp_get_group_name( $current_group );
         }else{
            //we are on any of group internal page

            $trail[]= "<a href='".  bp_get_group_permalink( $current_group )."'>".  bp_get_group_name( $current_group )."</a>";

            $subnav_details = hibuddy_find_subnav_details( $current_group->slug, $action );


            $trail_end = $subnav_details['name'];

            if( !empty( $action_variables ) ){
                $trail[] = "<a href='". $subnav_details['link']."'>".$subnav_details['name']."</a>";



                $trail_end = array_pop( $action_variables );
                foreach( $action_variables as $action_name )
                    $trail[] = ucwords( str_replace('-', ' ', $action_name ) );

               $trail_end = ucwords(str_replace('-', ' ',$trail_end ));


            }


        }



    }

//that's all folks for the User/group
//what other components do you use,
//I was planning to write the 3rd section to handle the non core components having their own single pages(e.g. events etc)
// but I need some suggestion about one of the plugins to see if I can have a generic way of doing it
    //append trail end
     if( !empty( $trail_end ) )
            $trail['trail_end'] = $trail_end;

     return $trail;

}
/**
 * 
 * Iterates over a multi dimensional array and finds the array item that has a slug name which matches item name
 * @global type $bp
 * @param mixed $collection array of nav items
 * @param type $item_name slug name to match against
 * @return mixed|boolean
 */
function hibuddy_find_nav_details($collection, $item_name){



    foreach( $collection as $nav_item){

        if($nav_item['slug']== $item_name){

            //let us manipulate the link
            if ( bp_loggedin_user_domain() ) 
            $nav_item['link'] = str_replace( bp_loggedin_user_domain(), bp_displayed_user_domain(), $nav_item['link'] );

            return $nav_item;

        }
    }

    return false;
}
/**
 * Find the subnav array 
 * @global type $bp
 * @param type $component : component name groups etc
 * @param type $action which action eg. admin/forums etc
 * @return mixed array of sub nav item details 
 */
function hibuddy_find_subnav_details( $component, $action ){
    global $bp;

    $main_nav = $bp->bp_options_nav[$component];


    return hibuddy_find_nav_details( $main_nav, $action );

}

#buddypress #breadcrumb #bp-hacks

by Brajesh Singh

Permalink: #190

Remove Friendship Button on User Profile(removes add/remove/Cancel Friendship button from user profile)

//remove friendship button oon Single User profile
add_action('bp_init','bp_custom_remove_friendship_btn_on_profile');
function bp_custom_remove_friendship_btn_on_profile(){

    if ( has_action( 'bp_member_header_actions', 'bp_add_friend_button' ) ) {
            remove_action( 'bp_member_header_actions', 'bp_add_friend_button', 5 );

    }

}

#buddypress #friends #bp-hacks