• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Tips and Tricks » Creating a login link with redirect back to the original page

Creating a login link with redirect back to the original page

Chad Butler · Feb 9, 2015 ·

Often times, the WP-Members plugin may be used in a theme or other situation where there is already a log in link. A lot of these types of default log in links direct to the main WP login. But they often have the nice feature of redirecting back to the referring page. This example will show how to set up a WP-Members login page, create a link to this page, and include a redirect back to the referring page.  

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Tips and Tricks filters, login_url, wp_login_url

Welcome to RocketGeek Interactive › Forums › Creating a login link with redirect back to the original page

Tagged: filters, login_url, register redirect, wp_login_url

  • This topic has 2 replies, 2 voices, and was last updated 5 years ago by Chad Butler.
Viewing 1 reply thread
  • Author
    Posts
    • February 9, 2015 at 7:25 pm #7153
      Chad Butler
      Keymaster

      Often times, the WP-Members plugin may be used in a theme or other situation where there is already a log in link. A lot of these types of default log
      [See the full post at: Creating a login link with redirect back to the original page]

    • April 5, 2016 at 6:00 am #9861
      GreenJames
      Participant

      Hi there,
      Thanks for this tip; it’s been useful.

      But to get it to work as I expected it to, i.e. to return me to the page I was on where I clicked the login link, I found I had to add something to it, like this:

      add_filter( 'login_url', 'my_login_page', 10, 2 );
      function my_login_page( $login_url, $redirect ) {
      	if( isset( $_SERVER['REQUEST_URI'] ) && 0 != strlen( $_SERVER['REQUEST_URI'] ) ) {
              $redirect = esc_url( $_SERVER['REQUEST_URI'] );
      	}
      	$redirect_to = '?redirect_to=' . get_permalink();
          return home_url( '/log-in/?redirect_to=' . $redirect );
      }

      I hope that will be useful to someone else.

      I’ve also been looking at your other tip Redirecting WordPress urls for login, logout, and registration.

      I’m trying to repeat what I have done for the login link for the registration link, but it doesn’t work. As you haven’t covered it in these tips, does that indicate it’s not possible to do for the registration form?

      This is my code that I’m trying and is returning an error:

      add_filter( 'register_url', 'my_register_url' );
      function my_register_url( $register_url, $redirect ) {
      	if( isset( $_SERVER['REQUEST_URI'] ) && 0 != strlen( $_SERVER['REQUEST_URI'] ) ) {
              $redirect = esc_url( $_SERVER['REQUEST_URI'] );
      	}
      	$redirect_to = '?redirect_to=' . get_permalink();
      	return home_url( '/register/?redirect_to=' . $redirect );
      }

      Can we get the registration form to redirect in the same way?

      BTW, I am also using your other snippet to log the user in after successful registration.

      • April 5, 2016 at 7:26 am #9862
        Chad Butler
        Keymaster

        I’m not sure about the registration redirect – it may be possible. I’ve been doing some work in that area to fix some general issues with the parameter from the shortcode. At present, that doesn’t work as it should and it is being addressed in the next release (3.1) which I expect to release early next week (the beta build is available here).

        There is a difference in the two processes (login and registration) that affects this, and using a redirect with the registration currently results in not displaying error messages for fields (or if the process fails) and since most people use this in a way that ends the user on a “thank you” page, the process really isn’t suited for that the way it is.

        So… consider that an open topic (both one I’m working on expanding the feature set as well as open to suggestions).

        You might want to also look at the Advanced Options extension. This extension incorporates some of the snippets you mentioned into a simple extension.

        It’s fairly new and I haven’t had a chance to go back through all of the content to indicate that the feature is available in the extension. The code snippets will definitely stay up on the site because for a more advanced use or something that requires more control over the process, the snippet will give that to you whereas the extension gives you a basic “check a box to turn it on/off” kind of process. But for the basic general implementation, the extension is easy to set up.

        Hope that helps.

  • Author
    Posts
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy