Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danbp/8d10eab2895953ee6289 to your computer and use it in GitHub Desktop.
Save danbp/8d10eab2895953ee6289 to your computer and use it in GitHub Desktop.
/*
* Tested on BP 2.3.2.1 / Twenty Twelve
* A similar BP profile menu as under Howdy, to add to main menu.
* Code goes into bp-custom.php
*/
function bpfr_nav_menu_profile_link( $menu ) {
global $bp;
// determine user to use
$current_user = wp_get_current_user();
$user_id = bp_get_member_user_id();
// print out username
$name = sprintf( '%1$s', $current_user->user_login );
// fetching url's
$profile_url = bp_loggedin_user_domain();
$notifications_link = trailingslashit( $user_domain . bp_get_notifications_slug() );
// rebuilding partially the notification counter which is originally aside Howdy. Only the total count, no details in sub-menu.
$notifications = bp_notifications_get_notifications_for_user( bp_loggedin_user_id(), 'object' );
$count = ! empty( $notifications ) ? count( $notifications ) : 0;
$alert_class = (int) $count > 0 ? 'pending-count alert' : 'count no-alert';
$notif_title = '<span id="ab-pending-notifications" class="' . $alert_class . '">[ ' . number_format_i18n( $count ) . ' ]</span>';
$notif_link = trailingslashit( bp_loggedin_user_domain() . bp_get_notifications_slug() );
// get user's avatar
$avatar = bp_core_fetch_avatar( array(
'item_id' => bp_loggedin_user_id(),
'width' => 16,
'height' => 16,
'class' => 'avatar',
)
);
// menu only for logged-in user
if( is_user_logged_in() ) {
// notif items, with count
$count = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
if ( ! empty( $count ) ) {
$notif = sprintf( _x( 'Notifications <span class="count">%s</span>', 'My Account Notification pending', 'buddypress' ), number_format_i18n( $count ) );
$unread = sprintf( _x( 'Unread <span class="count">%s</span>', 'My Account Notification pending', 'buddypress' ), number_format_i18n( $count ) );
} else {
$notif = _x( 'Notifications', 'My Account Notification', 'buddypress' );
$unread = _x( 'Unread', 'My Account Notification sub nav', 'buddypress' );
}
// messages items, with count
$count = messages_get_unread_count();
if ( !empty( $count ) ) {
$msg = sprintf( __( 'Messages <span class="count">%s</span>', 'buddypress' ), number_format_i18n( $count ) );
$inbox = sprintf( __( 'Inbox <span class="count">%s</span>', 'buddypress' ), number_format_i18n( $count ) );
} else {
$msg = __( 'Messages', 'buddypress' );
$inbox = __( 'Inbox', 'buddypress' );
}
// the menu itself
$profilelink = '
<li class="menu-item-800" style="margin-right:0px!important;"><a href="' . $notif_link . '">'. $notif_title .'</a></li>
<li class="menu-item-900"><a href="' . $profile_url . '">'. $name .' '. $avatar .'</a>
<ul class="sub-menu">
<li class="menu-item" id="menu-item-901"><a href="' . $profile_url . 'activity">'. __('My Activities', 'buddypress') .'</a></li>
<li class="menu-item" id="menu-item-902"><a href="' . $profile_url . 'groups">'. __('My Groups', 'buddypress') .' </a>
<ul class="ab-submenu">
<li id="create-group">'. bp_get_group_create_button() .'</li>
</ul>
</li>
<li class="menu-item" id="menu-item-903"><a href="' . $profile_url . 'notifications">'. $notif .' </a>
<ul class="ab-submenu">
<li id="unread-notif"><a href="' . $profile_url . 'notifications">'. $unread .'</a></li>
<li id="read-notif"><a href="' . $profile_url . 'notifications/read">'. __('Read', 'buddypress') .'</a></li>
</ul>
</li>
<li class="menu-item" id="menu-item-904"><a href="' . $profile_url . 'messages">'. $msg .' </a>
<ul class="ab-submenu">
<li id="custom-inbox"><a href="' . $profile_url . 'messages/inbox">'. $inbox .'</a></li>
<li id="custom-compose"><a href="' . $profile_url . 'messages/compose">'. __('Compose', 'buddypress') .'</a></li>
</ul>
</li>
</ul>
</li>';
$menu = $profilelink . $menu;
}
return $menu;
}
add_filter( 'wp_nav_menu_items', 'bpfr_nav_menu_profile_link', 10, 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment