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

Disable Product Pages in WooCommerce

Add the following code to your functions.php file to disable users from accessing product pages within WooCommerce: /* Remove Links to Access Product Pages */ remove_action( ‘woocommerce_before_shop_loop_item’, ‘woocommerce_template_loop_product_link_open’, 10 ); remove_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_product_link_close’, 5 ); /* Prevent Users from Accessing Product Pages by Displaying the 404 Page */ function prevent_access_to_product_page(){

Read More »
Smart Web Creative - Knowledge Base - How to Recover WordPress Additional CSS

How to Recover WordPress Additional CSS

How to Recover WordPress Additional CSS in 2023 (Step-by-Step) – If you have ever lost additional CSS within your WordPress theme, we’ll show you how you can most likely recover it in this step-by-step tutorial. First, we will go over what CSS is, how you can add custom CSS in

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 »

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