16 April 2012

Edit BuddyPress Admin Bar - Log In

A site I've been working on is using a WordPress multisite install, with BuddyPress.  As a multisite install, the idea is users will be able to create their own blog, which works fine.  However, say a user who is not logged in, visits one of the sub-blogs and clicks the 'Log In' button on the BuddyPress admin bar, they're redirected to the main blog home page. Not optimal behaviour for me.
There was no option to edit this in the BuddyPress admin menu, no plugin to alter this, and trying to search for a solution turned up results that either asked how to fix it, or were about a slightly different problem (e.g. disabling the BuddyPress admin bar and creating one's own).

I have created a plugin to fix this- 'BuddyPress Adminbar Log In Fix' or 'BP-adminbar-c' for short.

To fix the log in behaviour of the button is not too difficult, we create a function for the button, active only when the user is not logged in:


function custom_adminbar_li_button() {
if (!is_user_logged_in()) {
echo '<li><a href="/wp-login.php/">Log In</a></li>';
}
}


Then, we create a function to remove the original log in button, and replace it with our new button, appending it to the WordPress footer which calls the BuddyPress admin bar:

function remv_bp_adminbar_li(){

remove_action('bp_adminbar_menus', 'bp_adminbar_login_menu', 2);
add_action('bp_adminbar_menus', 'custom_adminbar_li_button', 2);
}
add_action('wp_footer','remv_bp_adminbar_li',1);


This plug in is available on GitHub: http://coopey.me/3xci7