Redirect user back to content after Login and/or registration

I have created a "No Access Message" that has 2 links to either login or register "to see exclusive content". But I would like to have the message redirect the user back to the current page after logging in/registering (using a register page built into Membership). The "no access message" area doesn't support php. I was simply going to append the $_SERVER["REQUEST_URI"] to each of my login and register links.

Any ideas for a best solution would be greatly appreciated. Maybe through a filter on the no access message or something similar?

  • aware
    • Design Lord, Child of Thor

    Yeah I was thinking I could possibly do a combination of

    add_filter( 'login_url', 'mysite_login_url', 10, 2);


    add_filter("login_redirect", "my_login_redirect", 10, 3);

    and then add_query_arg('redirect_to', urlencode( $_SERVER['HTTP_REFERER'] ), $login_url);depending on a "redirect" action.

    The challenge is that I'd need to append the query arg based on a non dynamic query var


    if(isset($_GET["action"]) && $_GET["action"] == "redirect_after_login") {
     append the query arg

    Just some brainstorming. Any ideas your devs have would be greatly appreciated.

  • aware
    • Design Lord, Child of Thor

    So I ended up using a little javascript to accomplish what I needed for now from a "login" standpoint. I haven't tried to see if the redirect_to query arg actually works post registration within the membership plugin. But for the time being here is my hackish solution. I don't have a huge amount of time this morning so forgive any typos, grammatical other issues in this lil run down.

    Within my options No Access Message I have the following.

    Please <a href="/wp-login.php" class="download-login">login</a> or <a href="/registration/" class="download-login">create an account</a> in order to access this content.

    The registration href is a page created in the site that has the user registration/subscription form.

    Within my functions.php I added in a "redirect" variable into a wp_localize_script "reorder" object that I already had defined.

    wp_localize_script( 'core', 'reorder', array(
    			'ajaxurl'	=> admin_url( 'admin-ajax.php' ), // URL to wp-admin/admin-ajax.php to process the request
    			'nonce'		=> wp_create_nonce( 'reorder-nonce' ),
    			'uid'		=> $current_user->ID,
    			'view'		=> esc_js( $_GET['view'] ),
    			'redirect'  => site_url( $_SERVER['REQUEST_URI'] )

    If you wanted a barebones wp_localize you can remove the ajax url nonce uid view etc and rename the object to something that is relevant to your project.

    Now that we have a referable redirect uri we can now use a little bit of jquery to apply that redirect to our links.

    Within my no access message you will notice I have a class defined on each of my hrefs "download-login" we will be using this class name as a selector within jquery.

    jQuery(function($) {
    	$('.download-login').each(function() {
    		$(this).attr('href', $(this).attr('href') + '?redirect_to=' + reorder.redirect);

    This snippet edits the href attribute of ALL elements with the .download-login class and appends our reorder.redirect to the end.

    While this approach is NOT best method. If you need a way to redirect a user back to the page with your protected content on login. It should work. It was fine in my testing.

    I have NOT tested this after going through the full registration process yet. After I do some testing I will post my findings here if any one else needs a janky way to redirect users,

  • aware
    • Design Lord, Child of Thor

    redirect_to query argument doesn't carry over during the registration process. Kind of a bummer. @Phil is there any chance I could get that put in as a feature request? Or if I make an update to the plugin myself can I commit it back to you guys?

  • Mason
    • DEV MAN’s Sidekick


    Just checking if it was eventually resolved in another thread? Or by yourself separately to us? Or by us over email with you? Or using our live support?

    If so, no need to reply, that's great news.

    If not, could you let us know by re-opening this topic, and we'll get onto it and helping you out asap!
    Otherwise, happy days, glad you got it sorted :slight_smile:


  • aware
    • Design Lord, Child of Thor

    Well the issue was 2 fold. 1 was redirecting users on login. That portion I resolved with a bit of a hack that I described above. The 2nd issue is redirecting user after they register and subscribe. If there is a specific area of the site for feature requests let me know and I will post a request there.

    Just makes sense to us to redirect a user back to where they came from (especially if you are protecting content by shortcodes)

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.