0

I would like to add the language of every blog to the blogname inside the my-site-list inside the admin-bar.

The result should look like this: enter image description here

For adding the languages to to the site-name in (1) I use this code:

add_action('wp_before_admin_bar_render', 'add_language_to_blog_name');
function add_language_to_blog_name( $wp_admin_bar ) {

    global $wp_admin_bar;

    $wp_admin_bar->add_menu( array(
        'id'    => 'site-name',
        'title' => get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'language' ),
    ) );

}

How can I edit the blognames inside the my-site-list?

1 Answer 1

0

This is a little tricky, and I don't know if I got the best solution for this. However, it works.

The trick is, I use the same hook as you do, wp_before_admin_bar_render. In there I get all the elements, and check them if they have the parent my-sites-list.

If they do, I switch to the blog, get the language of the current blog, create the title and call restore_current_blog().

After that, I create a new admin bar item, containing all the original information but the new title, remove the old one and insert the new one right afterwards.

Issues/concernces

  • I could not find out by now if there is a way to get a blogs language without switching to it.
  • Maybe there is a filter for the generation of the admin bar menu items. It would be much better to filter those titles there than manipulating them afterwards.

The function

function f711_tweaked_admin_bar() {

    global $wp_admin_bar;
    $elements = $wp_admin_bar->get_nodes();
    foreach( $elements as $element ) {

        if ( $element->parent == 'my-sites-list' ) {

            // reduce the adminbar Id to the blog ID
            $idinteger = str_replace( 'blog-', '', $element->id );

            // Switch to the Blog, get the language option. If empty, it's English
            switch_to_blog( $idinteger );
                if ( get_option( 'WPLANG' ) != "" ) {
                    $language = get_option( 'WPLANG' );
                } else {
                    $language = 'en_EN';
                }
                //set the new title for the Admin Menu
                $newtitle = $element->title . ' - ' . $language;
            restore_current_blog();

            // define new menu element
            $args = array(
                'title'     => $newtitle,
                'id'        => $element->id,
                'parent'    => $element->parent,
                'href'      => $element->href,
            );
            // remove the old one
            $wp_admin_bar->remove_node( $element->id );
            // add the new one right afterwards.
            $wp_admin_bar->add_node( $args );

        }

    }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.