The MU forums have moved to WordPress.org

Subscriber Redirect (5 posts)

  1. stacef
    Member
    Posted 14 years ago #

    I'm preparing to transfer a suite of sites from individual Wordpress installations to WPMU and having trouble with a small piece of code.

    I currently have a function that redirects to the home page any Subscriber trying to access the admin menus. Looks like this:

    /*
    * redirect subscribers to front page if they try to access wp-admin pages
    */
    function redirect_subscribers() {
    	global $user_level;
    	if ($user_level == 0) {
    		wp_redirect(get_option('siteurl'));
    	}
    }
    add_action('admin_menu', 'redirect_subscribers');

    Simple and it works, at least in the individual installations. In WPMU, however, the redirect sends the user to the parent url, ie http://www.mydomain.com/wpmu instead of http://www.mydomain.com/wpmu/blog_I_was_just_looking_at. The conditional checks out ok; the trouble seems to be in the redirect. It appears that the admin menu function pulls the user's primary blog data and replaces any variables in wp_redirect.

    I've tried setting the variables both inside the function and outside (making sure to include it as a global of course) and I've tried various get_option, get_bloginfo and get_current_site calls with no success. I tried just creating a simple variable like $var = 'my_blog' but even that won't pass in to the redirect. Hardcoding the url in that function does work.

    What am I missing here? Or, is there a better way to accomplish keeping subscribers out of wp-admin?

    Any thoughts or ideas appreciated.

  2. DeannaS
    Member
    Posted 14 years ago #

    I tried your code exactly as it is. It worked for me. At least, it worked if I'm understanding what you're trying to do. (I'm using sub-directories here, but I tested on subdomains, too and it worked just fine.)

    So, if I'm on:
    http://mydomain.com/test

    and I go to http://mydomain.com/test/wp-admin

    I get redirected back to
    http://mydomain.com/test/

  3. stacef
    Member
    Posted 14 years ago #

    Thanks very much for trying!

    I get the same results as you did with a similar test but I think I'm not being clear ... sorry.

    If I'm at http://www.mydomain.com/test/site1
    and I go to http://www.mydomain.com/test/site1/wp-admin
    I get redirected to http://www.mydomain.com/test
    instead of my desired http://www.mydomain.com/test/site1

    Basically, I just want to disable access to wp-admin by redirecting to the home page of whatever blog is currently being viewed (site1, site2, etc).

    I think part of the deal is that the admin menu function uses the user's primary blog - which is indeed useful if you have admin access. You login and go to "your" blog. But it seems to be short-circuiting what I'm trying to do.

    If you find the time to test again, I'd sure appreciate it.
    Thanks!

  4. wpmuguru
    Member
    Posted 14 years ago #

    A user is only allowed to access the admin area of a blog they have a role on. So, MU is redirecting the user to the main blog before your plugin gets to them.

  5. stacef
    Member
    Posted 14 years ago #

    wpmuguru:
    thanks for the reply - your explanation makes sense.

    Any ideas on how I might accomplish what I'm after?

About this Topic