Shortcode for Pulling Current Page URL | Elementor Form Example

Smart Web Creative - Shortcode for Pulling Current Page URL

With WordPress, there may be times when you need to use a shortcode for pulling the current page URL. One scenario that is pretty common for this is when you use a global Elementor form that needs to have a dynamic link for a “Thank You” page.

Global Elementor Form Example

When you use an Elementor form widget, it gives you the option to have a redirect after a user submits the form. Now, this is great if the link you are placing for the redirect is static. However, from a marketing perspective, especially when you use Google conversion tracking codes, you should redirect the user to a “Thank You” page. This “Thank You” page should be unique (have a unique URL). But what if you have multiple pages that use the same exact form? You would need to have the redirect field be somewhat dynamic. For example, let’s say we have two pages that are using the same form. These pages have the following URLs:

  1. www.mysite.com/page-a/
  2. www.mysite.com/page-b/

The “Thank You” page URLs should follow these pages as so:

  1. www.mysite.com/page-a/thank-you/
  2. www.mysite.com/page-b/thank-you/

The main part of these URLs that stay the same is “thank-you/”. The rest of the URL before that will always be different. Since this is the case, we need to create a shortcode that can pull in the current page URL to place before the “thank-you/”.

Creating the Shortcode

Copy the following shortcode and paste it into your theme’s functions.php file:


/* Shortcode for Pulling Current Page URL */
add_shortcode ('geturl', 'get_current_page_url');
function get_current_page_url() {
    $obj_id = get_queried_object_id();
    $current_url = get_permalink( $obj_id );
    return $current_url;
}
/* End Shortcode for Pulling Current Page URL */

You need to understand that “geturl” is the name of the shortcode. You can change this to whatever you’d like, but it needs to be the same for when you use the shortcode.

Using the Shortcode

The next step is to head over to your Elementor form. Be sure you have a Redirect option selected under Actions After Submit. In the Redirect dropdown, make the Redirect To option set to Shortcode. You will want to click on the Shortcode selection and add “[geturl]”. In the Advanced area, add “thank-you/” in the After field. Your redirect URL is essentially this: [geturl]thank-you/ or using our example above, www.mysite.com/page-a/thank-you/.

Smart Web Creative - Shortcode for Pulling Current Page URL Example 01 Smart Web Creative - Shortcode for Pulling Current Page URL Example 02

 

 

 

 

 

 

 

 

 

Now you should know how to use a shortcode for pulling the current page URL. This can come in handy for other scenarios as well.

Other Helpful Articles

What is Webmail?

What is webmail? Webmail is usually a free cloud-based email service that is set up on your hosting. You can use webmail by having a professional-looking email address such as, “[email protected]” and it’s usually at no additional cost. You can also have a professional-looking email with an email client such

Read More »

Online Generator for Writing a Privacy Policy

Many websites need a privacy policy page. WordPress actually has a pre-built privacy policy page that users can edit. However, editing what they give you can be a little overwhelming. Furthermore, depending on your business, it may be better to use a lawyer to write up a more detailed privacy

Read More »
Smart Web Creative - Elementor: Create Conditional Form Fields with Code

Elementor: Create Conditional Form Fields with Code

If you ever wanted to create conditional form fields with code using Elementor Pro, this tutorial will guide you on how to do that. We will be using an example where users will be asked if they would like to enter their address details. It will look like this once

Read More »

What Do You Need Done?:

(Please check all that apply)

Tell Us About You

Almost Done!

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info