1. Customize
  2. BuddyPress Integration
  3. Force to use BuddyPress displayed user instead of user logged in

Force to use BuddyPress displayed user instead of user logged in

Some themes offers the possibility to add a sidebar to the BuddyPress user profile. At this sidebar, GamiPress shortcodes and widgets that has set to show current user earnings will show earnings based on current logged in user, not the current displayed user.

The following snippet shows how to force to set as current user the BuddyPress displayed user.

function my_prefix_replace_logged_in_user_by_displayed_user( $out, $pairs, $atts ) {

    // Just override user_id if current_user is set to yes and if we are on an BuddyPress user profile
    if( bp_displayed_user_id() !== 0 && isset( $atts['current_user'] ) && $atts['current_user'] === 'yes' ) {

        $out['user_id'] = bp_displayed_user_id();

        // Force current user to "no" to prevent being overwritten after
        $out['current_user'] = 'no';
    }

    return $out;

}
// Hook for [gamipress_achievements] shortcode and GamiPress: Achievements widget
add_filter( 'shortcode_atts_gamipress_achievements', 'my_prefix_replace_logged_in_user_by_displayed_user', 10, 3 );
// Hook for [gamipress_points] shortcode and GamiPress: User Points widget
add_filter( 'shortcode_atts_gamipress_points', 'my_prefix_replace_logged_in_user_by_displayed_user', 10, 3 );
// Hook for [gamipress_rank] shortcode and GamiPress: Ranks widget
add_filter( 'shortcode_atts_gamipress_ranks', 'my_prefix_replace_logged_in_user_by_displayed_user', 10, 3 );
// Hook for [gamipress_user_rank] shortcode and GamiPress: User Ranks widget
add_filter( 'shortcode_atts_gamipress_user_rank', 'my_prefix_replace_logged_in_user_by_displayed_user', 10, 3 );
// Hook for [gamipress_logs] shortcode and GamiPress: Logs widget
add_filter( 'shortcode_atts_gamipress_logs', 'my_prefix_replace_logged_in_user_by_displayed_user', 10, 3 );
// Hook for [gamipress_earnings] shortcode and GamiPress: User Earnings widget
add_filter( 'shortcode_atts_gamipress_earnigns', 'my_prefix_replace_logged_in_user_by_displayed_user', 10, 3 );
Was this article helpful to you? Yes No

How can we help?