List subsites from mainsite

I would like to hide the admin bar in my subsites and list a subsite for the site admins from the main site, for the main site I created the function:

if ( is_user_logged_in() && is_admin()) {

add_filter('wp_nav_menu_items', 'my_sites', 10, 2);
function my_sites($items, $args) {
$items .= '<li> <a href="/wp-admin/my-sites.php"> My Sites </a></li>';
return $items;
}
}

To add a menu item when the user is logged, but I’m having a problem to send the link to a subsite URL

It would depend on the admin ID to create a URL like subsite/wp-admin/my-sites.php

Any Idea?

  • Dimitris
    • Support Star

    Hello there Iker :slight_smile:

    Please try the following snippet. You can use it as is, like a MU plugin, meaning in a new PHP file like:

    wp-content/mu-plugins/my-sites.php (just create the /mu-plugins/ folder, if doesn’t exist already).

    <?php
    function display_my_sites() {
    if ( is_user_logged_in()) {
    function my_sites($items, $args) {
    // https://codex.wordpress.org/Function_Reference/wp_get_current_user
    $current_user = wp_get_current_user();
    // https://codex.wordpress.org/WPMU_Functions/get_blogs_of_user
    $user_blogs = get_blogs_of_user($current_user);

    foreach ($user_blogs as $user_blog) {
    $items .= '<li><a href="'.$user_blog->siteurl.'/wp-admin/my-sites.php"> My Sites </a></li>';
    }
    return $items;
    }
    add_filter('wp_nav_menu_items', 'my_sites', 9999, 2);
    }
    }
    add_action('init', 'display_my_sites');

    Thank you,

    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.