WordPress.org

Support

Support » Plugins and Hacks » WP-Members: Membership Framework » [Resolved] Redirect to refferer after login

[Resolved] Redirect to refferer after login

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

    @cbutlerjr

    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).

    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

    Any help with this Chad?

    Thanks,
    Luke

    Plugin Author Chad Butler

    @cbutlerjr

    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.

    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

    Plugin Author Chad Butler

    @cbutlerjr

    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

    Plugin Author Chad Butler

    @cbutlerjr

    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:

    Dynamic redirect to referrer in login

    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

    Plugin Author Chad Butler

    @cbutlerjr

    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.

    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

    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?

    Plugin Author Chad Butler

    @cbutlerjr

    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.

    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=&lt;?php echo “http://” . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]; ?>

    Is that possible?

    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

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

    Thanks,
    Luke

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Resolved] Redirect to refferer after login’ is closed to new replies.
Skip to toolbar