Wednesday, 8 May 2013

Admin bar for non logged users

Its possible to display the admin bar for the non-logged-in users it can be easily done by using the show_admin_bar filter and admin_bar_menu hook.

Just paste the following code in either plugin files or in the functions.php ( theme file ).

if(!function_exists('custom_login_adminbar')){
    function custom_login_adminbar( $wp_admin_bar) {


        if ( !is_user_logged_in() )
        $wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );

/*Here you can add your own menus*/
    }


}
add_action( 'admin_bar_menu', 'custom_login_adminbar' );
add_filter( 'show_admin_bar', '__return_true' , 1000 );


 

By default the admin bar will come with wordpress documentation link and search box. You can remove the admin bar items by using wp_before_admin_bar_render filter. Following code will remove the wordpress documentation link for non-logged-in user.

Just paste the following code in either plugin files or in the functions.php ( theme file ).

    if(!function_exists('custom_admin_bar_render')) {
        function custom_admin_bar_render() {


            if ( !is_user_logged_in() ) {
                global $wp_admin_bar;
                $wp_admin_bar->remove_menu('wp-logo');
            }


        }
    }
add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_render' );

No comments:

Post a Comment