WordPress.org

Ready to get started?Download WordPress

Forums

WP-Members
[resolved] Redirect to refferer after login (24 posts)

  1. Luke Etheridge
    Member
    Posted 12 months ago #

    Hi there,

    Just want to say what an amazingly helpful plugin this is!

    I'm trying to direct users to the refferer after they've logged in, so...

    - User is on Page A, clicks login.
    - User is on Login page. Logs in.
    - User is directed back to Page A.

    Is this possible and if so any help/tips would be appreciated.

    Here is the code I'm currently using to direct users to the homepage after login...

    add_filter( 'wpmem_login_redirect', 'my_login_redirect' );
    
    function my_login_redirect()
    {
    	// return the url that the login should redirect to
    	return site_url();
    }

    Also, any tips on making the form fields responsive (percentage widths) would be great.

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

  2. Chad Butler
    Member
    Plugin Author

    Posted 12 months ago #

    If you set up the plugin in the default setting, you don't have to worry about redirecting the user. The login form displays in place of blocked content if the user is not logged in already. So rather than the steps you have described, it would go like this:

    - User is on Page A, logs in
    - User is on Page A in logged in state

    You can make the form more responsive through customizing the the stylesheet, and you can also create a filter function if you need to change specific form elements (see: Customizing Forms in the User Guide).

  3. Luke Etheridge
    Member
    Posted 12 months ago #

    Hey Chad,

    Thanks for the reply.

    I'm not actually blocking whole posts or pages so I do not have a login form in place of the content for the way that I'm using it.

    I'm mainly using the plugin to use is_user_logged_in on certain sections of a page or custom post type. Changing certain buttons based on the users login status.

    Therefore I've setup a standalone login page (called 'Login') using your shortcode for the user to be able to login. At present, once the user has logged in it just directs them back to that page where it tells them they're 'logged in as admin' (for example).

    Is there a way that I can still have this standalone login page but have it redirect to the refferer once the user has logged in? I.e. the last page they were on before arriving at the 'Login' page.

    Basically the idea is that I don't want them to lose the place that they were last and have to scramble through quite a large site to find it again.

    Thanks again Chad and I look forward to your reply.
    Luke

  4. Luke Etheridge
    Member
    Posted 11 months ago #

    Any help with this Chad?

    Thanks,
    Luke

  5. Chad Butler
    Member
    Plugin Author

    Posted 11 months ago #

    The standalone login via the shortcode can accept a redirect_to parameter via querystring, much like the default wp-login.php.

    So, to redirect back on login, you would need to generate the link to the login page with the extra parameter. Something like this:

    http://yoursite.com/login/?redirect_to=http://yoursite.com/original-page

    If you do that at the template/code level, you could use get_permalink() to get the redirect parameter.

  6. Luke Etheridge
    Member
    Posted 11 months ago #

    Hi Chad,

    Thanks for getting back to me.

    I'm not quite sure I fully understand? Where exactly am I putting this code?

    Also, the referring page will be different all the time. There won't be the chance to specify it like you have done above.

    Cheers,
    Luke

  7. Chad Butler
    Member
    Plugin Author

    Posted 11 months ago #

    Hi Luke,

    I can give you some guidance, but I can't write a custom solution for you here. What you need is some custom function that will apply the redirect_to parameter to your links to the login page. That would most likely live in your functions.php file.

    If you need to do this automatically, then I would probably setup a content filter that looks through your content for a link to the login page and replaces it with the redirect_to parameter. Since that would occur when the content is being generated, you could probably use get_permalink to retrieve the needed parameter.

    Some info on building a content filter:
    http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

    and get_permalink:
    http://codex.wordpress.org/Function_Reference/get_permalink

  8. Chad Butler
    Member
    Plugin Author

    Posted 11 months ago #

    Every once in while, I do like to put up a "freebie" on the premium support site to show examples of customizations and tutorials. This seemed like a good one to add there, so here's some cut-and-paste code for you:

    http://rocketgeek.com/filter-hooks/dynamic-redirect-to-referrer-in-login/

  9. Luke Etheridge
    Member
    Posted 11 months ago #

    Chad you're a legend for sorting this and for your free advise.

    I hate to keep on hounding you but I put that code in my functions.php and nothing seemed to change? I.e. login page still directs to itself once logged in.

    Thanks again for your efforts with this Chad, it's really appreciated.

    Luke

  10. Chad Butler
    Member
    Plugin Author

    Posted 11 months ago #

    You need to troubleshoot where it's breaking down. Look at the link that is being generated. Does it have the redirect_to= parameter being added to the end? If not, the breakdown is somewhere with the_content filter. If it's got redirect_to= but ends there with no url as the parameter, the breakdown is with get_permalink. Figuring out where to go from there depends on which of those is the issue.

  11. Luke Etheridge
    Member
    Posted 11 months ago #

    Chad,

    I see and I now understand what you've done.

    To make this even more complicated my login page is linked to from within my header.

    I can confirm that it definitely DOES work for all links to the login page that are within my main content.

    I'm assuming it's only a small change to have this redirect work outside of the_content but within the header?

    Thanks,
    Luke

  12. Luke Etheridge
    Member
    Posted 11 months ago #

    In fact, if there is a way that you can change all links to the login page across the ENTIRE site that would be awesome.

    I am basically linking to the login page in the sidebars, excerpts, footer, header etc, etc...

    Hope that makes sense Chad?

  13. Chad Butler
    Member
    Plugin Author

    Posted 11 months ago #

    That makes more sense - the header is outside The Loop, so get_permalink is not available.

    The simple answer is to use the post ID past to get_permalink:

    global $post;
    $link = get_permalink( $post->ID );

    The problem with that alone is that on an archive page, you'll probably get the ID of the first post in a series of posts. What you'll need to do to make it clean is to probably use php's switch with a case for various things. If it's is_page or is_post, you can use the above, but if it is is_archive, is_search, or is_category, you'll need to work out a process for those. Documentation on those functions is in the WP Codex.

  14. Luke Etheridge
    Member
    Posted 11 months ago #

    Chad,

    Adding this to the login page's URL seems to work

    ?redirect_to=<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

    BUT I would prefer not to have to do that manually to all of the links and rather have something in functions.php that searches for the link across the WHOLE site and replaces it with the above extension.

    In simpler terms...

    1) Search for all occurrences of http://www.mysite.com/login

    2) Replace with http://www.mysite.com/login?redirect_to=<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

    Is that possible?

  15. Luke Etheridge
    Member
    Posted 11 months ago #

    Also, whilst we're in conversation could you let me know how I can have the sidebar widget redirect to the current page after successful login.

    It's also not showing any error messages on unsuccessful login? It just directs to the homepage regardless of success or failure.

    Thanks,
    Luke

  16. Luke Etheridge
    Member
    Posted 11 months ago #

    Hey Chad, just wondering if you had a fix for the above issue with the widget?

    Thanks,
    Luke

  17. Luke Etheridge
    Member
    Posted 11 months ago #

    Chad,

    Just thought I'd let you know that I've PHP coded in a solution to my template files which kind of fixes the directing to refferer after login. I just need to have the widget automatically direct to the current page after login. Any ideas?

    Thanks,
    Luke

  18. Chad Butler
    Member
    Plugin Author

    Posted 11 months ago #

    Hi Luke,

    In most cases, you can get the current location with get_permalink. But since a widget can/will show on other non-single pages, such as archives, search results, home page, etc, you will need to create a case to handle those properly as well. If you use get_permalink on an archive page such as a list of posts by month, get_permlink will return the first link in the loop.

  19. Luke Etheridge
    Member
    Posted 11 months ago #

    Chad,

    Yes I know about this issue, and I know there is PHP that can be used to simply get the current page's URL. Something which is outside of the WordPress Codex like...

    http://webcheatsheet.com/PHP/get_current_page_url.php

    All I need to know is what I need to do to actually get the widget to direct to the current page on 'submit'? Like is there something that I can put into functions.php that will specifically target the widget form and not all other WP Members forms?

    I was using this before, but clearly this targets all WP Members forms...

    add_filter( 'wpmem_login_redirect', 'my_login_redirect' );
    
    function my_login_redirect()
    {
    	// return the url that the login should redirect to
    	return site_url();
    }
  20. Luke Etheridge
    Member
    Posted 10 months ago #

    Am I right in thinking you've fixed the widget to redirect to the current page in the latest update?

    If so, thanks Chad you're a legend!

    Luke

  21. Chad Butler
    Member
    Plugin Author

    Posted 10 months ago #

    Hi Luke -

    Both the sidebar and the body login have always been set up to put the user on the page that they logged in on. However, in certain circumstances this wasn't correctly picked up in the sidebar (mostly if WP was set up in a different directory that the site).

    So, in essence, yes, this should fix that issue with the widget.

    But I want to be careful with wording here. It's not that it "fixed the widget to redirect to the current page" since we are not really "redirecting". It is doing what it is supposed to do which is put the user where they were. If we take them away from there, that would be redirecting (which is what the redirect filter would appropriately be used for).

    Hope that clarifies...

  22. Luke Etheridge
    Member
    Posted 10 months ago #

    Chad,

    It does and that's what I meant by 'redirect' I just used the wrong terminology ;)

    All I know is that it was redirecting to the homepage after login no matter where the user logged in from on the site, and now it keeps them on the page they were viewing before signing in.

    Thanks again for all your help Chad.

    Luke

  23. Luke Etheridge
    Member
    Posted 10 months ago #

    Hey Chad,

    One last question (at least I think so).

    The site I've been developing with your plugin is here - http://www.sportscluboffers.co.uk

    For this question if you could please go here and click on the 'Login or Register' button for one of the offers.

    Now, I've made it so that as soon as the visitor 'logs in' with the login form, they redirect to the full page of the offer they were logging in for (redirected to the permalink). But what I want to know is how to do exactly this if the visitor successfully 'registers' (so is a non-member) using the registration form below it?

    So only upon successful registration does it not only register but log them in and take them back to that offers full listing (permalink).

    This sounds far fetched but like anything I'm sure it is possible in some way. Please let me know if this is achievable.

    Thanks Chad,
    Luke

  24. Luke Etheridge
    Member
    Posted 9 months ago #

    Hey Chad,

    Sorry to be a pest but is there any way to do this (above)?

    I appreciate any help with this bud.

    Luke

Reply

You must log in to post.

About this Plugin

About this Topic