Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Custom site titles for the My Sites network admin bar menu in a multisite network. Configure first function, then auto-activate by saving this file in wp-content/mu-plugins, or network activate as a regular plugin.
<?php
/**
* Plugin Name: Custom My Sites Menu Titles
* Description: Custom site titles for the My Sites network admin bar menu. Multisite only.
* Version: 2015.11
* Author: Caspar Hübinger
* Author URI: https://profiles.wordpress.org/glueckpress
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* THIS NEEDS TO BE ADJUSTED TO YOUR NEEDS!
*
* @return array Site IDs and site title customizations
*/
function ms_custom_site_menu_titles__custom_settings() {
/*
* USE SITE IDs FROM YOUR NETWORK HERE!
* Schema: existing site ID => new site title
*/
$your_network = array(
1 => array( 'site_menu_title' => 'Site DE' ),
70 => array( 'site_menu_title' => 'Site AT' ),
42 => array( 'site_menu_title' => 'Site CH' ),
);
return $your_network;
}
/**
* Initialize plugin.
*
* @return void
*/
function ms_custom_site_menu_titles__load() {
// Logged in multisite users only.
if ( ! is_user_logged_in() || ! is_multisite() )
return;
// Remove core version of My Sites menu.
remove_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
// Add custom copy of My Sites menu, including new filter.
add_action(
'admin_bar_menu',
'ms_custom_site_menu_titles__admin_bar_my_sites_menu',
21
);
// Filter and modify site titles for My Sites menu.
add_filter(
'ms_custom_site_menu_titles__admin_bar_network_site_title',
'ms_custom_site_menu_titles__custom_site_menu_titles',
10,
4
);
}
add_action( 'plugins_loaded', 'ms_custom_site_menu_titles__load' );
/**
* Assign custom site titles for network sites.
*
* @param string $site_title Default menu item text
* @param string $blavatar An empty div by default
* @param string $blogname Site title
* @param string $userblog_id Site ID
* @return string Modified or default menu item text
*/
function ms_custom_site_menu_titles__custom_site_menu_titles( $site_title, $blavatar, $blogname, $userblog_id ) {
// Fetch custom network settings from top of file.
$your_network = ms_custom_site_menu_titles__custom_settings();
// Sanitize filter argument.
$current_site = absint( $userblog_id );
// Assign new site title.
if ( isset( $your_network[ $current_site ] ) ) {
$site_title = $blavatar . $your_network[ $current_site ][ 'site_menu_title' ];
}
return $site_title;
}
/**
* Exact copy of the "My Sites/[Site Name]" menu as in WordPress 4.4-beta2,
* except 1 filter has been added (ms_custom_site_menu_titles__admin_bar_network_site_title).
*
* @see wp-includes/admin-bar.php
* @param object $wp_admin_bar Admin bar object
* @return void
*/
function ms_custom_site_menu_titles__admin_bar_my_sites_menu( $wp_admin_bar ) {
// Don't show for logged out users or single site mode.
if ( ! is_user_logged_in() || ! is_multisite() )
return;
// Show only when the user has at least one site, or they're a super admin.
if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
return;
if ( $wp_admin_bar->user->active_blog ) {
$my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' );
} else {
$my_sites_url = admin_url( 'my-sites.php' );
}
$wp_admin_bar->add_menu( array(
'id' => 'my-sites',
'title' => __( 'My Sites' ),
'href' => $my_sites_url,
) );
if ( is_super_admin() ) {
$wp_admin_bar->add_group( array(
'parent' => 'my-sites',
'id' => 'my-sites-super-admin',
) );
$wp_admin_bar->add_menu( array(
'parent' => 'my-sites-super-admin',
'id' => 'network-admin',
'title' => __('Network Admin'),
'href' => network_admin_url(),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'network-admin',
'id' => 'network-admin-d',
'title' => __( 'Dashboard' ),
'href' => network_admin_url(),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'network-admin',
'id' => 'network-admin-s',
'title' => __( 'Sites' ),
'href' => network_admin_url( 'sites.php' ),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'network-admin',
'id' => 'network-admin-u',
'title' => __( 'Users' ),
'href' => network_admin_url( 'users.php' ),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'network-admin',
'id' => 'network-admin-t',
'title' => __( 'Themes' ),
'href' => network_admin_url( 'themes.php' ),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'network-admin',
'id' => 'network-admin-p',
'title' => __( 'Plugins' ),
'href' => network_admin_url( 'plugins.php' ),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'network-admin',
'id' => 'network-admin-o',
'title' => __( 'Settings' ),
'href' => network_admin_url( 'settings.php' ),
) );
}
// Add site links
$wp_admin_bar->add_group( array(
'parent' => 'my-sites',
'id' => 'my-sites-list',
'meta' => array(
'class' => is_super_admin() ? 'ab-sub-secondary' : '',
),
) );
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
switch_to_blog( $blog->userblog_id );
$blavatar = '<div class="blavatar"></div>';
$blogname = $blog->blogname;
if ( ! $blogname ) {
$blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
}
$menu_id = 'blog-' . $blog->userblog_id;
$site_title = $blavatar . $blogname;
$wp_admin_bar->add_menu( array(
'parent' => 'my-sites-list',
'id' => $menu_id,
// Adding a new filter here.
'title' => apply_filters(
'ms_custom_site_menu_titles__admin_bar_network_site_title',
$site_title,
$blavatar,
$blogname,
$blog->userblog_id
),
'href' => admin_url(),
) );
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-d',
'title' => __( 'Dashboard' ),
'href' => admin_url(),
) );
if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-n',
'title' => __( 'New Post' ),
'href' => admin_url( 'post-new.php' ),
) );
}
if ( current_user_can( 'edit_posts' ) ) {
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-c',
'title' => __( 'Manage Comments' ),
'href' => admin_url( 'edit-comments.php' ),
) );
}
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-v',
'title' => __( 'Visit Site' ),
'href' => home_url( '/' ),
) );
restore_current_blog();
}
}
@glueckpress

This comment has been minimized.

Copy link
Owner Author

glueckpress commented Nov 16, 2015

Please take note: Replacing a rather large core function only to implement a missing filter should not be understood as good practice. The code above turned out as the result of support topic and should be seen rather as a temporary hack than as anything long-term. Even though the menu tweak is implemented as a plugin, one will have to monitor the admin_bar_menu core filter and it’s arguments in the future when using this plugin on a productive site.

@bueltge

This comment has been minimized.

Copy link

bueltge commented Nov 16, 2015

Hint if you use MultilingualPress

I see in the first method the defining of language specific sites in the network. If you will pursue this with the MultilingualPress plugin, then is it a better alternative to filter the name of each site with the custom site name of each site.

@glueckpress

This comment has been minimized.

Copy link
Owner Author

glueckpress commented Nov 20, 2015

@bueltge Thanks, very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.