WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Can Polylang filter WP Admin bar too? (3 posts)

  1. miguelcortereal
    Member
    Posted 7 months ago #

    More specifically the link from WP admin bar 'site-name' de one that now with WP 3.8 displays the house icon and allows the access to dashboard if in frontend or visit the site if in backend.

    Visit the site link should be redirecting to backend language home page instead default language home page.

    http://wordpress.org/plugins/polylang/

  2. miguelcortereal
    Member
    Posted 7 months ago #

    I've figured out a way to make wp toolbar home link point to home page in the correct language.

    I'm using default language without language slug.

    What I did was paste at child theme functions.php the whole function defined at wp core that adds this particular menu and sub menu items to wp toolbar and apply some changes.

    home_url('/') was replaced by variable $home_url which assumes the correct value according the language.

    function change_wp_admin_bar_site_menu( $wp_admin_bar ) {
            // Don't show for logged out users.
            if ( ! is_user_logged_in() )
                    return;
    
            // Show only when the user is a member of this site, or they're a super admin.
            if ( ! is_user_member_of_blog() && ! is_super_admin() )
                    return;
    
            $blogname = get_bloginfo('name');
    
            if ( empty( $blogname ) )
                    $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
    
            if ( is_network_admin() ) {
                    $blogname = sprintf( __('Network Admin: %s'), esc_html( get_current_site()->site_name ) );
            } elseif ( is_user_admin() ) {
                    $blogname = sprintf( __('Global Dashboard: %s'), esc_html( get_current_site()->site_name ) );
            }
    
            $title = wp_html_excerpt( $blogname, 40, '…' );
    
            // Set the correct language for home link
            	global $wp_admin_bar, $polylang;
            	$locale = get_locale();
    			$language = $polylang->model->get_language($locale);
    			$home_url = get_term_link($language->slug, 'language');
    			if ( $home_url == home_url().'/en/') {
    				$home_url = home_url();
    			}
    
            $wp_admin_bar->add_menu( array(
                    'id'    => 'site-name',
                    'title' => $title,
                    'href'  => is_admin() ? $home_url : admin_url(),
            ) );
    
            // Create submenu items.
    
            if ( is_admin() ) {
                    // Add an option to visit the site.
                    $wp_admin_bar->add_menu( array(
                            'parent' => 'site-name',
                            'id'     => 'view-site',
                            'title'  => __( 'Visit Site' ),
                            'href'   => $home_url,
                    ) );
    
                    if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
                            $wp_admin_bar->add_menu( array(
                                    'parent' => 'site-name',
                                    'id'     => 'edit-site',
                                    'title'  => __( 'Edit Site' ),
                                    'href'   => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
                            ) );
                    }
    
            } else {
                    // We're on the front end, link to the Dashboard.
                    $wp_admin_bar->add_menu( array(
                            'parent' => 'site-name',
                            'id'     => 'dashboard',
                            'title'  => __( 'Dashboard' ),
                            'href'   => admin_url(),
                    ) );
    
                    // Add the appearance submenu items.
                    wp_admin_bar_appearance_menu( $wp_admin_bar );
            }
    }
    
    add_action('wp_before_admin_bar_render', 'change_wp_admin_bar_site_menu');
  3. Chouby
    Member
    Plugin Author

    Posted 7 months ago #

    Great. Thanks for sharing. That may help other users interested in this feature.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.