Page 1 of 1

Login redirection broken for users with Author capabilities

PostPosted: October 26th, 2010, 6:24 pm
by arwyl
Because I want all registered users to be able to blog, I have edited the capabilities of the 'Subscriber' and 's2member level 1' roles with Capability Manager and given them Author permissions. Now, when users log in, they are no longer being redirected to my specified Welcome page, but end up on the home page instead. How do I fix this?

Thanks very much in advance!

Re: Login redirection broken for users with Author capabilit

PostPosted: October 27th, 2010, 3:41 am
by Jason Caldwell
Thanks for the excellent question.

Well, I wish we had a Hook/Filter that would accomplish this for you. I'll see about adding a Filter in the next release so the method I'm about to describe won't be necessary. Until then, here goes.

Unzip the file that I've attached, and place it into your /mu-plugins/ directory. If you don't have an /mu-plugins/ directory, please create one. MUST USE (mu)-plugins are loaded automatically.

s2-login-redirection.zip
(969 Bytes) Downloaded 4 times

I've added this additional check: $user->has_cap("author")
Code: Select all
<?php
/*
Create this function before s2Member loads it's default version.
    goes here: /wp-content/mu-plugins/s2-login-redirection.php
*/
function ws_plugin__s2member_login_redirect ($username = FALSE)
    {
        eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
        do_action ("ws_plugin__s2member_before_login_redirect", get_defined_vars ());
        unset ($__refs, $__v); /* Unset defined __refs, __v. */
        /**/
        $username = (!$username && is_object ($current_user = wp_get_current_user ())) ? $current_user->user_login : $username;
        /* This additional check was added in case wp_signon() fires this event with empty $_POST credentials.
            In this rare case, we can check to see if WordPress® is remembering a previously logged in User. */
        /**/
        if (!is_object ($user = new WP_User ($username)) || ! ($user_id = $user->ID) || !$user->has_cap ("edit_posts") || $user->has_cap ("author"))
            {
                eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
                do_action ("ws_plugin__s2member_during_login_redirect", get_defined_vars ());
                unset ($__refs, $__v); /* Unset defined __refs, __v. */
                /**/
                if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_password"] && function_exists ("ws_plugin__s2member_generate_password"))
                    {
                        delete_user_setting ("default_password_nag"); /* setcookie() */
                        update_user_option ($user_id, "default_password_nag", false, true);
                    }
                /**/
                if (strtolower ($username) === "demo" || ws_plugin__s2member_ip_restrictions_ok ($_SERVER["REMOTE_ADDR"], strtolower ($username)))
                    {
                        if ($special_redirection_url = ws_plugin__s2member_login_redirection_url ($user))
                            wp_redirect ($special_redirection_url);
                        /**/
                        else
                            wp_redirect 
(get_page_link ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_welcome_page"]));
                    }
                /**/
                exit ();
            }
        /**/
        eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
        do_action ("ws_plugin__s2member_after_login_redirect", get_defined_vars ());
        unset ($__refs, $__v); /* Unset defined __refs, __v. */
        /**/
        return;
    }
?>

Re: Login redirection broken for users with Author capabilit

PostPosted: October 27th, 2010, 8:30 am
by arwyl
Thank you, Jason, for your prompt reply!

I had already been taking a look at that line in the \wp-content\plugins\s2member\includes\functions\login-redirection.inc.php file, but I hadn't been able to figure out what the right edit to the check was.

Curiously, the check that did the trick was the opposite one:
Code: Select all
if !$user->has_cap ("author")


Maybe because my users are not "proper" Authors, but Subscribers and s2-level-1 members with added capabilities that give them author rights. I'm not sure I understand :? , but it sure is working! Both Subscribers and s2-level-1 members now get redirected to my custom Welcome page, so I can gratefully mark this issue as:

RESOLVED.

Thanks again! :D