Gravity Forms Login Redirect Shortcode

Gravity FormsWe use Gravity Forms on almost every WordPress site we’ve built since 2008.

Recently, when migrating from Squarespace to self-hosted WordPress to take advantage of Woocommerce and AffiliateWP, we were asked to build a form which would let their European wholesale reps create new wholesale accounts for their clients.

So we built a gravity forms registration form, tying it in to the Woocommerce Dynamic Pricing extension.

For privacy, we wanted the form to only be available to logged in users (in the Gravity Forms form settings, under ‘Restrictions’ check “Require user to be logged in”), not only to prevent spam but also for security reasons, and we also put a custom validation code field that only they know the answer to. Finally, the form lives on a URL that is no-index, no-follow so (hopefully) it shouldn’t appear in search engines.

For ease of use though, the problem facing the wholesale reps was the login requirement. If they didn’t login first, they’d hit the page, then have to go to the WordPress login URL, then after login go back to that page. With WordPress’ wp_login_url function, we can take care of 90% of that. But how to add this functionality to the form’s “Require Login Message” field since Gravity Forms doesn’t allow PHP (for security reasons) in the field?

Enter a custom shortcode:

// Add Shortcode for redirect to login

function ci_loginRedirect( $atts , $content = null ) {
	$currentURL = wp_login_url( get_permalink() );

	 return "<a href='$currentURL' title='Login'>";
add_shortcode( 'ci_redirect_to_login', 'ci_loginRedirect' );

function ci_closeLoginRedirect( $atts , $content = null ) {
	 return '</a>';
add_shortcode( 'ci_close_login', 'ci_closeLoginRedirect' );

Add that to your theme’s functions.php file (if it’s a child theme, otherwise you’ll have to create one first to make sure this code isn’t overwritten).

Then, in the Gravity Forms settings for that form, we add our new shortcodes:

Gravity Forms Settings

And we’re good to go!

You Must Login


Hit us up with questions below.

The best Affiliate Marketing plugin for WordPress

Why AffiliateWP? We’re so impressed by AffiliateWP, we’ll do a dedicated post this fall, once the American Schizandra team has had more time to put it through its paces during their October product launch. But in terms of choosing this plugin over others, the decision was primarily based on AffiliateWP’s extensive selection of addons, and the ability from outside developers to add their own addons for AffiliateWP. This means the functionality will always be evolving to meet the needs of customers.

In this case specifically, the Lifetime Commissions and Stripe integration was the dealmaker – lifetime commissions ability is very rare, or even unique to this plugin. Pushover notifications is a cool added bonus. But there’s enough addons with advanced functionality (recurring referrals for membership products, Buddypress integration, etc.) that we feel confident we can use AffilateWP for whatever client needs arise in the future.

For a complete list of addons, visit AddiliateWP’s addon page [affiliate link, what else!].


  1. M7 on October 24th, 2016 at 4:13 pm

    Hey there. Suppose I want the redirect to take the user to the User Registration page on my site rather than the WordPress login? What would I need to change in the code?

    • john on January 2nd, 2017 at 2:10 pm

      Hi @M7 —

      The easiest way would be to remove line 4 and in line 6, just replace the href with whatever URL you’d like to send them to… so something like:

      href='/my-site-registration/' title='User Registration'

  2. Kathy on June 5th, 2017 at 3:58 pm

    Hi John,
    Thank you for your solution. Unfortunately, I’ve added your code to my child theme’s function and I’ve added the shortcode: [ci_redirect_to_login]Login[ci_close_login] in the form setting require login message but it isn’t working. The shortcode is displaying on the front end.

    • john on June 5th, 2017 at 6:05 pm

      Hi Kathy – it should be working, there isn’t anything that’s changed in WordPress which would not make it work. So you definitely added the code to the end of your child theme’s functions.php file? One easy way you could test if it’s in the right place is to remove the last two lines – just end it with return '';… this should actually break your site if it’s in the right place. If it doesn’t break the site (you get a white screen or an error message) then it’s in the wrong place. In other words, just delete the two lines from what you have, resave the file, check the site in your browser and see what happens. If the site still works, it’s in the wrong place. If it doesn’t work then try copying the whole thing again and pasting it in again… just to double-check there isn’t something missing. What’s happening essentially is somehow WordPress isn’t being told to create that custom shortcode.

      You have the other two steps right for sure.

      If you still can’t get it working, send me a screenshot of the location of the file (in your FTP software or the WP admin) and what you added to the file (the code) and I’ll take a look for you. 🙂

  3. Luke Cavanagh on September 15th, 2017 at 2:40 pm

    Would recommend either adding a plugin header and turning it into a site specific plugin or using a plugin like Code Snippets to add that function to.

  4. marco on September 15th, 2017 at 4:37 pm

    I would like to go to wp-login and then to another page, not the previous
    how and where can I modify your shortcode?

Leave a Comment