Using query vars in shortcodes

While refreshing my WordPress maintenance site, The WP Butler, I came across quite a specific need. When a user signed up for service, I wanted to forward them to the client area so that they could immediately start accessing their services.

However, after some testing it became apparent that after a user signed up, it wasn’t terribly obvious that their signup was successful, or what they should do next. Instead, they were just presented with a faceless login page. While seasoned clients would know what to do at this point, it’s an awkward stumbling block for a new signup.


I wanted to create a message that I could add to this page so that new signups would get something of a confirmation that their signup was successful, and that they can login with their login details to access their account. However, because this page is used by all clients, I didn’t want to show the message all the time.

Adding the query var when needed

I came up with the idea of using query vars to solve this issue. My signup form was created using Gravity Forms, so my idea was to add a query var to the confirmation, which was set to redirect to a page (the client area) after submitting the form:


Now when a user signed up they would be sent to, instead of just where existing clients could access their account. Both addresses will pull up the same content: it’s just that the first has some extra information that WordPress can tap into.

Using the query var

In order to allow WordPress to use custom query vars, such as the one I just created (‘signup’), we need to add it to the list of query vars that WordPress can use. We do this with the query_vars filter.

Now that WordPress can actually use the new query var, I want to create a shortcode that will display the enclosed content only if the query var is present. That is, users visiting will see the message, and anyone visiting (or any other variation without the ?signup=success query var) will not.

The code for this shortcode looks like this. In short, WordPress looks for the query var ‘signup’, and if it is equal to ‘success’, then it will return the content of the shortcode. Otherwise, it will display nothing.

All of this code goes in my functionality plugin. With the code in place, I can now craft the content for my client area page.

Since I use Restrict Content Pro to restrict access to certain portions of my site, the following code is what determines what non-logged-in users see (everything enclosed by the [not_logged_in] shortcode):

You will notice that above the login form ([login_form]) is my new shortcode that displays a message (using the Symple Shortcodes plugin for styling) when the query var is present. So now, when users sign up, Gravity Forms tacks on my custom query var, and my new shortcode interprets that as a need to display a message to the user. So new users now see this when they’re redirected to the client area after signup: much better!


Leave a Reply