Code Edit Redirect Multisite Path

I hope this is an easy adjustment for someone to make. Sorry this my only work-around that I've even come close to figuring out for redirecting to the correct back end that might work for the time being.

marketpress > flexmarket

Using this in my MU-PLUGINS it was allowing all new users to use a link to get to their correct back end but it was making the users an editor(as indicated.)

I need the users to be subscriber of the main site and admin of their own site. They need to be able to access their shops settings.

(link to info)
http://www.intechgrity.com/automatic-primary-site-in-wordpress-multisite-set-blog-creating-users-primary-creation/#

Code that needs adjustment:

<?php

/**
* 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_editor;

/**
* 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_editor 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_editor = true, $make_subscriber = true) {
$this->make_primary = $make_primary;
$this->make_editor = $make_editor;
$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_editor && $user_id != 1)
add_user_to_blog ($blog_id, $user_id, 'editor');

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();

//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);

  • aristath
    • Recruit

    Hello again @Veg,

    By default in WordPress when a new user registers on your site they become subscribers and if they create a new site they are admins of their own site.

    What is it exactly that you want to accomplish using the above code? What is your goal exactly? Because I have a feeling you don't need that at all!

    Please advise,
    Ari.

  • Veg
    • Site Builder, Child of Zeus

    @aristath

    A .php file that I can place in my MU-PLUGINS

    A code that makes any new users a subscriber of the mainsite and admin of their own site (not editor.) Redirecting their primary page to their new shop/blog that they just created at all times.

  • Veg
    • Site Builder, Child of Zeus

    @Webmaster I'm hoping @aristath will know a simple modification to this code to give a decent work around for the time being. I have no clue about what something like that would cost. I assumed marketpress would have had this done when I got it. To make it easy for the user to get to their correct back-end, but again I'm in marketing not web dev. (I thought I just had it set up wrong for the first few days of designing the site.)

    @aristath please still help with this php :slight_smile:

  • Veg
    • Site Builder, Child of Zeus

    @aristath is there a way to tweak this code? To create all new shop owners to be the admin of their site? Then the marketpress multi-site can function with less confusion. At least there will be a link that users can click to get to the correct backend and they can also edit their 'store settings' if they are their own shop admins. which they need to be. I cannot user "multisite user management" plugin with this code.

  • aristath
    • Recruit

    @Veg
    Store owners are already the admins of their own sites, and they already are subscribers on the main site.
    What you need is not assigning roles to users.... what you need is a simple redirection to the user's site!

    Remove the code you posted on your original post and try this:

    <?php
    
    function my_login_redirect( $redirect_to, $request, $user ){
      //is there a user to check?
      global $user;
    
      if ( $user ) {
        $user_id = $user->ID;
      }
    
      if ( isset( $user_id ) ) {
        // Get the blogs of this user
        $user_blogs = get_blogs_of_user( $user_id );
        // Get the URL of the blog
        foreach ( $user_blogs AS $user_blog ) {
          return $user_blog->domain;
        }
        // If user has no blog, return to the homepage.
      } else {
        return home_url();
      }
    }
    add_filter( "login_redirect", "my_login_redirect", 10, 3 );

    I haven't tested it but it should work...

    I hope that helps!

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.