WordPress.org

Forums

WP-Members
[resolved] Redirect after login (27 posts)

  1. CreativeFedora
    Member
    Posted 1 year ago #

    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)
    	{
    		'url-to-sample-page';
    	}
    	if($user_id == 2)
    	{
    		'url-to-sample-page-2';
    	}
    }

    http://wordpress.org/plugins/wp-members/

  2. Chad Butler
    Member
    Plugin Author

    Posted 1 year ago #

    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';
    	}
    }
  3. CreativeFedora
    Member
    Posted 1 year ago #

    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: http://www.opinionsearchusa.com/

    Login: user1
    Pass: user1

  4. Chad Butler
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  5. CreativeFedora
    Member
    Posted 1 year ago #

    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' ) . '" />
    </div>
    </form>';
    }
  6. respectyoda
    Member
    Posted 1 year ago #

    If you are having trouble coding, use this plugin.

    Peter's Login Redirect Plugin

  7. CreativeFedora
    Member
    Posted 1 year ago #

    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.

  8. respectyoda
    Member
    Posted 1 year ago #

    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?

  9. CreativeFedora
    Member
    Posted 1 year ago #

    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.

  10. respectyoda
    Member
    Posted 1 year ago #

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

  11. CreativeFedora
    Member
    Posted 1 year ago #

    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.

  12. respectyoda
    Member
    Posted 1 year ago #

    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.

  13. CreativeFedora
    Member
    Posted 1 year ago #

    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?

  14. respectyoda
    Member
    Posted 1 year ago #

    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 />
  15. CreativeFedora
    Member
    Posted 1 year ago #

    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?

  16. respectyoda
    Member
    Posted 1 year ago #

    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.

  17. CreativeFedora
    Member
    Posted 1 year ago #

    Amazing! I really appreciate all your help. Thanks. I've learned quite a bit about php just tinkering with WP-Members and now Peter's plugin.

  18. respectyoda
    Member
    Posted 1 year ago #

    No problem! I wish you the best with your project. :)

  19. Audiv Produccions
    Member
    Posted 1 year ago #

    Dear All,

    Could you tell me How I can to redirect to the page that I want, one user when him is logged?

    I want to make some pages that only one registred client could see.

    Best regards

  20. respectyoda
    Member
    Posted 1 year ago #

    Hello Audiv,

    If you aren't using this redirect plugin, download it.

    Peter's Login Redirect Plugin

  21. Audiv Produccions
    Member
    Posted 1 year ago #

    Thanks for you answer, I will install it and I will inform you.

  22. respectyoda
    Member
    Posted 1 year ago #

    Audiv,

    No problem. If you have any questions, do not hesitate to drop in and inquire.

  23. Chad Butler
    Member
    Plugin Author

    Posted 1 year ago #

    @Audiv Produccions - it's probably better to start a new thread than to come in on an existing support thread tagged to a specific plugin.

    If your question is how do you redirect logins for the WP-Members plugin, there is a filter hook for that - wpmem_login_redirect. You can find this and all other hooks documented in the plugin's user guide.

  24. Audiv Produccions
    Member
    Posted 1 year ago #

    Dear Respectyoda, I have intalled the Peter's Logic Redirect Plugin and It's running OK, A lot of thanks.

  25. Audiv Produccions
    Member
    Posted 1 year ago #

    Dear Chad Butler, I wolud like to run the wpmem_login_redirect, but I was reading it, but I don't know where I have to put the php code?
    If you can help me please.
    Thanks

  26. respectyoda
    Member
    Posted 1 year ago #

    Audiv, good to hear! You are welcome. Also, like Chad said, please open a new thread with your question in it. Thanks.

  27. Audiv Produccions
    Member
    Posted 1 year ago #

    Ok,

    I do it.

    Best regards

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.