Multisite variable link to admin panel

First, Merry Christmas and thank you for taking your time to help!

Second, so I am trying to create a variable link so that users can access there admin panel.

I want to put a link in the admin bar titled "Home"
So here is how it works. A user logs in. The admin bar pop up. The link title "Home" is in the admin bar. They are able to click on that and it takes them to their admin panel NOT the main site's admin panel.
Ex. mysite.com/UserSite/wp-admin/
NOT mysite.com/wp-admin

How do I create a variable link that will pull the site name associated with the username that is logged in. It can't pull the username because users can have a different username form their site name.
Ex. I can pick User as my username and pick MyShop as the site name.
So the link has to be able to pull the MyShop (site name) portion into the link NOT the User (username) portion.

Thank you and Happy Holidays!

  • Vinod Dalvi

    Hi josh,

    Wish you a Merry Christmas and thank you for your question.

    Doesn't the default WP admin bar fulfill your requirements?

    The default WP admin bar in multi site also displays admin panel links for sub sites along with main site's admin panel in front and back end.

    Also check whether the following plugin meets your requirements or not.

    https://premium.wpmudev.org/project/custom-admin-bar/

    Please advise.

    Kind Regards,
    Vinod Dalvi

  • josh

    Hi @Vinod Dalvi

    Thanks for your reply. Unfortunately the default admin bar isn't what I'm looking for.
    And that plugin is great thank you, but I don't think it solves my problem

    The "custom admin bar" plugin does allow me to have a custom logo and that is what I already have.

    Basically I want my custom logo to link to the users admin panel not to the main site's admin panel. I want to disable the "my sites" menu, but before I can do that I need to be able to create a variable link that I can use for my logo so when users click the logo they will be taken to their admin panel. Ex. mysite.com/usersite/wp-admin

    I hope that makes sense.

    Thank you

  • Vinod Dalvi

    Hi josh,

    Thank you for your reply.

    Basically I want my custom logo to link to the users admin panel not to the main site's admin panel.

    Are you talking about logo icon image attached to the admin bar menu item?

    I want to disable the "my sites" menu

    You can disable it by installing Custom Admin Bar plugin and then from the following path select "My Sites" checkbox in "Disable WordPress menu items" section

    Network Admin -> Settings -> Custom Admin Bar

    but before I can do that I need to be able to create a variable link that I can use for my logo so when users click the logo they will be taken to their admin panel. Ex. mysite.com/usersite/wp-admin

    After installing Custom Admin Bar plugin visit the custom admin bar settings page then create a Custom entry having set "Title link leads to" option to "Site Admin area"

    Best Regards,
    Vinod Dalvi

  • josh

    Hi again,

    After installing Custom Admin Bar plugin visit the custom admin bar settings page then create a Custom entry having set "Title link leads to" option to "Site Admin area"

    This doesn't work. It takes the user to the main site's dashboard, not their own.

    Ex. This is where they go ----- mysite.com/wp-admin/
    This is where I need them to go ---- mysite.com/TheirSite/wp-admin

    This is why I need a variable link or syntax. So that when a user clicks on it it generates a link based on the site name associated with their username.
    Ex. mysite.com/TheirSite/wp-admin
    The "TheirSite" portion of the link is what has to be variable becuase every user has their own site name.

    So how can we do this?

    thanks

  • josh

    Are you talking about logo icon image attached to the admin bar menu item?

    Yes! :slight_smile:

    But if I can't have the variable link with the logo, I can settle for just a normal custom entry in the admin bar that does the same thing.

    Ex. Here at WPMU Dev the admin bar (my admin bar) has the WPMU Dev logo. This is where I want users to be able to click on and it takes them to their own site's admin area.
    If this can't be done (though I'm pretty sure it can be), I will settle for just a custom entry that does the same thing.
    Ex. In the WPMU Dev admin bar there are custom entries titled "Community" "Ask a Question" etc..
    If I can get one of these to do the variable link that would be fine.

    Thanks

  • Vinod Dalvi

    Hi josh,

    You can achieve the desired functionality by adding following code in functions.php file of your child theme or using plugin like
    http://wordpress.org/plugins/add-actions-and-filters/

    add_action( 'admin_bar_menu', 'modify_admin_bar' );
    
    function modify_admin_bar( $wp_admin_bar ){
    	$blog = get_active_blog_for_user( get_current_user_id() );
    	$blog_url = $blog->siteurl.'/wp-admin';
    
    	$defaults = array(
    		'href' => $blog_url,
    		'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu.
    		'id' => false, // defaults to a sanitized title value.
    		'title' => 'Site Admin Area',
    		'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', 'target' => '', 'title' => '' );
    	);
    
      $wp_admin_bar->add_menu( $defaults );
    }

    Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Hi josh,

    It's working fine for me and it is redirecting users on clicking admin bar custom link to the admin panel not to the main site's admin panel.

    The get_active_blog_for_user() function returns the user's primary blog, if she has one and it is active. If it's inactive, function returns another active blog of the user. If none are found, the user is added as a Subscriber to the Dashboard Blog and that blog is returned.

    Have you tested it by setting proper Primary Site?

    User can set the primary blog from the path:

    Admin -> Dashboard -> My Sites -> Primary Site

    Please advise.

    Regards,
    Vinod Dalvi

  • josh

    Can someone chime in on this please? :slight_smile:

    Is there a way, to make the users site their primary site by default?
    So when they sign up for their site, the site they created would be their primary site.

    And also, I plan to only allow registered users to buy due to limiting reviews, so if a user just registers but doesn't create a site, then opens a site later I would like it to do the same thing. Not sure if this bit of info matters.

    Thanks

  • josh

    Thank you for your help Vinod!

    I found a different link that helped. Here is the code I used. It was originally meant to do other things in addition to the automatic primary site, but at the bottom it tells you how to only implement the automatic primary site. So I'm sure someone can clean this up and condense it by deleting the unnecessary code.

    /**
     * The Automatic Primary Site & Editor Class
     * Automatically sets the new site as the creating users primary site/blog
     * Automatically adds the creating user to the created site as editor
     * Automatically adds the new users to the main site as subscriber
     */
    class itg_auto_primary_editor {
        /**
         * Whether or not to set the created site as user's primary
         * Set it to false if you don't want to set
         * @var bool
         */
        var $make_primary;
    
        /**
         * Whether or not to make the creator as an admin of the site automatically
         * Set it to false if you don't wish to
         * @var bool
         */
        var $make_admin;
    
        /**
         * Whether or not to make the new users as subscriber to the main site
         * @var bool
         */
        var $make_subscriber;
    
        /**
         * The constructor
         * @param bool $make_primary Set true if you want to make the creating site primary for the user
         * @param bool $make_admin Set true if you want to make the creating user admin
         * @param bool $make_subscriber Set true if you want to add newly created users as subscriber to main site
         */
        public function __construct($make_primary = true, $make_admin = true, $make_subscriber = true) {
            $this->make_primary = $make_primary;
            $this->make_admin = $make_admin;
            $this->make_subscriber = $make_subscriber;
    
            add_action('wpmu_new_blog', array(&$this, 'do_action'), 100, 2);
            add_action('wpmu_new_user', array(&$this, 'subscribe_to_main'));
        }
    
        /**
         * Do the action when new site is created
         *
         * Make the site user's primary site
         * Make the user editor of her site
         * Make the user subscriber of the main site
         *
         * Should be hooked
         *
         * @param type $blog_id
         * @param type $user_id
         */
        public function do_action($blog_id, $user_id) {
            if($this->make_admin && $user_id != 1)
                add_user_to_blog ($blog_id, $user_id, 'admin');
    
            if($this->make_primary && $user_id != 1)
                update_user_meta ($user_id, 'primary_blog', $blog_id);
    
            if($this->make_subscriber && $user_id != 1)
                add_user_to_blog('1', $user_id, 'subscriber');
        }
    
        /**
         * Do the action when new user is created
         *
         * Make users subscriber to the main site
         *
         * Should be hooked
         *
         * @param int $user_id
         */
        public function subscribe_to_main($user_id) {
            if($this->make_subscriber && $user_id != 1)
                add_user_to_blog('1', $user_id, 'subscriber');
        }
    
    }
    
    //do all the actions
    $itg_new_blog = new itg_auto_primary_editor(true, false);
    
    //only set the primary site
    //$itg_new_blog = new itg_auto_primary_editor(true, false);
    
    //only set the editor
    //$itg_new_blog = new itg_auto_primary_editor(false, true);
    
    //dont set subscriber
    //$itg_new_blog = new itg_auto_primary_editor(false, true, false);

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.