Support » Plugin: WP-Members: Membership Framework » Redirect after login

  • Resolved CreativeFedora


    I’ve been working on tweaking the redirect filter hook so that it’ll redirect users to specific pages. This is what I got going on. It seems on the correct path and when I use it the login goes trough to the specified page but it’s blank.

    add_filter( 'wpmem_login_redirect', 'my_login_redirect' );
    function my_login_redirect()
    	if($user_id == 1)
    	if($user_id == 2)

Viewing 15 replies - 1 through 15 (of 26 total)
  • Plugin Author Chad Butler


    Try this:

    add_filter( 'wpmem_login_redirect', 'my_login_redirect' );
    function my_login_redirect()
    	$user_id = get_current_user_id();
    	if($user_id == 1) {
    		return 'url-to-sample-page';
    	if($user_id == 2) {
    		return 'url-to-sample-page-2';

    Thanks. The same situation happens with your code Chad, the login goes through but directs to a blank version of the home page. If I refresh the page, and after clicking to resend the info via the box that comes up, it loads up the correct page.

    Try it out:

    Login: user1
    Pass: user1

    Plugin Author Chad Butler


    It appears that you’ve done some customization to develop the header login but you have no value for the form’s post action and the hidden field redirect_to (which would generally be the same parameter).

    If you’ve made modifications to the code, that’s probably your issue.

    This is what the form code I’m using looks like:

    add_filter( 'wpmem_sidebar_form', 'my_sidebar_form_filter' );
    function my_sidebar_form_filter( $form ) {
    return '
    <form name="form" method="post" action="' . $post_to . '">
    <p>Please log in here if you have received a mailing asking you to prequalify</p>
    <div id="user-input">
    Username: <input type="text" name="log" class="username" id="username" />
    Password: <input type="password" name="pwd" class="password" id="password" />
    <input type="hidden" name="rememberme" value="forever" />
    <input type="hidden" name="redirect_to" value="' . $post_to . '" />
    <input type="hidden" name="a" value="login" />
    <input type="hidden" name="slog" value="true" />
    <input type="submit" name="Submit" class="buttons" value="' . __( 'login', 'wp-members' ) . '" />

    If you are having trouble coding, use this plugin.

    Peter’s Login Redirect Plugin

    Thanks Yoda. Peter’s plugin works well, but I do need the pages that I’m redirecting to to be password protected. I can password protect the pages via WP’s post/page publishing options but that forces the user to login to be redirecting and then input a password to see the page.

    You are welcome.

    Do you mean some pages will have a different password than others or are the pages only dependent upon the user login?

    My goal is to have 5 different users with respective passwords that when a user logs in they are directed to the specified page.

    I’m able to get 90% done with WP-Members but after the login from the homepage the redirect leads to a blank homepage. Upon refreshing the blank page the redirect goes through.

    With Peter’s Login Redirect plugin, you can specify redirects for different users.

    Correct, I’m able to have the 5 user/pass combos and redirect to the specified page. However, I need the the pages to be password protected.

    As of now each page is password protected via the post/page publishing options, but that requires a user to input an additional password once they are logged in and redirected to the specified page.

    What WP-Members allows me to do is add a user/pass combo and redirect to the specific protected page without having to input an additional password.

    The goal is to avoid having a secondary password to view the page.

    Yes, I completely understand what you mean. One way to approach this is to do the Peter’s Login Redirect for specific usernames then don’t use the post/page publishing options. Instead, add code to test to see if a certain user is logged in and if not, post something like “Access restricted”. If the certain user is logged in the page will show up like it should.

    Thanks for following up on this matter. I’m unfamiliar with much of php but I’m sure I can tinker with it and figure it out.

    Where would I put the code to test if a user is logged in?

    No problem. In the php file, put the following code:

    <?php if (! is_user_logged_in() ) { ?><br />
    <p>Access Denied!</p><br />
    <?php } else { ?><br />
     //put page content here<br />
    <?php } ?><br />

    The function does not accept parameters and it would be unnecessary to use usernames because redirection will take care of the users. However, this may not prevent logged in users accessing other pages that are only for certain users. So if page A is only for User X and page B is only for User Y, and so on, use the second approach below to use the username of the user.

    <br />
    <?php global $current_user; //create a variable to store info for the user<br />
        get_currentuserinfo();//a wp query to retrieve the meta data of user from wp tables</p>
    <p>    if ($current_user->user_login == 'Fedora') { ?><br />
            <h1>Welcome Fedora!!</h1><br />
       <?php } else { ?><br />
            <h1>Go away!</h1><br />
        <?php } ?><br />
    <?php ?><br />

    Wow! Thanks much! I came across something similar while searching user_id in the WP codex. I think I’m ready to go.

    How would I add an additional user besides ‘Fedora.’ For instance I want 2 users to be able to view the same page?

    Yes, you can tweak the code to add an additional user like this.

    <?php global $current_user; //create a variable to store info for the user
        get_currentuserinfo();//a wp query to retrieve the meta data of user from wp tables ?>
    <?php if ($current_user->user_login == 'Fedora') { ?>
            <h1>Welcome Fedora!!</h1>
       <?php } elseif ($current_user->user_login == 'Another') { ?>
            <h1>Hello Another!</h1>
        <?php } else { ?>
           <h1>Go Away!</h1>
    <?php } ?>

    Now, if you want two users to be able to view the same page, use the following code.

    <?php global $current_user; //create a variable to store info for the user
        get_currentuserinfo();//a wp query to retrieve the meta data of user from wp tables ?>
    <?php if ($current_user->user_login == 'Fedora' || $current_user->user_login == 'Another') { ?>
            <h1>Welcome Fedora (Another) !!</h1>
       <?php } else { ?>
            <h1>Go away!</h1>
        <?php } ?>

    The || symbol means OR in PHP.

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘Redirect after login’ is closed to new replies.