Skip to content

Instantly share code, notes, and snippets.

@hnla
Last active December 17, 2015 06:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnla/5563278 to your computer and use it in GitHub Desktop.
Save hnla/5563278 to your computer and use it in GitHub Desktop.
A function to add the BuddyPress sidebar-me logged in username/avatar, plus site wide notice & a custom rendering of a users notifications displayed as a list e.g new @mentions, new private messages etc. On logout or logged out a login form is presented. The function can be placed in functions.php and the function call added to any sidebar file …
function hnla_bp_login_bp_sidebar_me() {
/**
* This function provides BP sidebar login form if logged out
* welcome message username & logout link if logged in,
* list of any new notification on your account,
* sitewide site notices if any posted by site admin.
*
* Add function to your function.php file
* call function in any sidebar file using:
*
* hnla_bp_login_bp_sidebar_me()
*
* @package BuddyPress
* @subpackage hnla-sidebar-login
* @version 1.0
*/
$bp = buddypress(); ?>
<div id="bp-sidebar-me-login">
<?php if ( is_user_logged_in() ) : ?>
<?php do_action( 'bp_before_sidebar_me' ) ?>
<div id="sidebar-me">
<div id="sidebar-me-user">
<a href="<?php echo bp_loggedin_user_domain() ?>">
<?php bp_loggedin_user_avatar( 'type=thumb&width=40&height=40' ) ?>
</a>
<p class="user-link clearfix"><span class="your-name"><?php echo bp_core_get_userlink( bp_loggedin_user_id() ); ?></span></p>
<p><a class="logout" href="<?php echo wp_logout_url( bp_get_root_domain() ) ?>"><?php _e( 'Log Out', 'buddypress' ) ?></a></p>
</div>
</div>
<div id="user-sidebar-notifications-menu">
<?php if( $notifications = bp_core_get_notifications_for_user( bp_loggedin_user_id() )) : ?>
<h3 class="notification-title logged-in-user">
<a href="<?php echo bp_loggedin_user_domain(); ?>" title="Go to your account or click the specific notice links below">
<?php _e( 'Notifications', 'buddypress' ); ?>
<span><?php echo count( $notifications ) ?></span>
</a>
</h3>
<?php if ( $notifications ) { ?>
<ul>
<?php
$counter = 0;
foreach($notifications as $notification ){
$alt = ( 0 == $counter % 2 ) ? ' class="alt"' : ''; ?>
<li<?php echo $alt ?>><?php echo $notification ?></li>
<?php $counter++;
}
}
?>
</ul>
<?php else: ?>
<h3 class="notification-title logged-in-user"><?php _e('No new notifications ', 'hnla') ?></h3>
<?php endif; ?>
<?php if ( bp_is_active( 'messages' ) ) : ?>
<?php bp_message_get_notices(); /* Site wide notices to all users */ ?>
<?php endif; ?>
<?php do_action( 'bp_sidebar_me' ) ?>
</div><!-- / #user-sidebar-notifications-menu -->
<?php do_action( 'bp_after_sidebar_me' ) ?>
<?php else: ?>
<?php do_action( 'bp_before_sidebar_login_form' ); ?>
<?php if ( bp_get_signup_allowed() ) : ?>
<div id="login_area">
<form name="login-form" id="sidebar-login-form" class="standard-form" action="<?php echo site_url( 'wp-login.php', 'login_post' ); ?>" method="post">
<label for="sidebar-user-login"><?php _e( 'Username', 'buddypress' ); ?></label>
<input type="text" name="log" id="sidebar-user-login" class="input" value="<?php if ( isset( $user_login) ) echo esc_attr(stripslashes($user_login)); ?>" />
<label for="sidebar-user-pass"><?php _e( 'Password', 'buddypress' ); ?></label>
<input type="password" name="pwd" id="sidebar-user-pass" class="input" value="" />
<p class="forgetmenot"><label><input name="rememberme" type="checkbox" id="sidebar-rememberme" value="forever" /> <?php _e( 'Remember Me', 'buddypress' ); ?></label></p>
<p><a href="<?php echo bp_get_signup_page() ?>">Register</a> | <a href="<?php echo wp_lostpassword_url( get_bloginfo('url') ); ?>">Lost your password?</a></p>
<?php do_action( 'bp_sidebar_login_form' ); ?>
<input type="submit" name="wp-submit" id="sidebar-wp-submit" value="<?php _e( 'Log In', 'buddypress' ); ?>" />
<input type="hidden" name="testcookie" value="1" />
</form>
</div>
<?php do_action( 'bp_after_sidebar_login_form' ); ?>
<?php endif; // end if signup allowed ?>
<?php endif; ?>
<?php }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment