Display Current Page URL in WordPress with Shortcode

current page url

What is a Current Page URL?

In web development, the Current Page URL refers to the complete web address of the page a user is currently viewing. This URL includes the protocol (http or https), the domain name (example.com), and the specific path or directory within the site that the page resides in (e.g., /about-us). Knowing and displaying the current page URL can be useful for various purposes such as debugging, sharing, or integrating with other web services.

Where to Use the Current Page URL

  1. Sharing Links: Provide users with a direct link to the current page for easy sharing.
  2. Form Redirection: Redirect users back to the current page after form submission.
  3. Analytics Tracking: Track user interactions on specific pages for better insights.
  4. Dynamic Content: Display different content based on the current page URL.
  5. SEO Purposes: Help in analyzing and optimizing specific pages.

How to Get the Current Page URL in WordPress

To display the current page URL in WordPress, you can use a custom shortcode. This involves adding a small PHP function to your theme’s functions.php file. Here’s a step-by-step guide to achieve this.

Step-by-Step Guide

Step 1: Add PHP Code to functions.php

First, you need to add a custom function to your theme’s functions.php file. This function will construct and return the current page URL. Follow these steps:

  1. Open the functions.php File: Navigate to your WordPress theme directory and open the functions.php file. You can do this via the WordPress admin dashboard or using an FTP client.
  2. Add the Function: Insert the following code snippet into the functions.php file. This code defines a function to get the current page URL and registers a shortcode for it.
// Add this code to your theme's functions.php file
function get_current_page_url() {
    // Get the host (domain)
    $host = $_SERVER['HTTP_HOST'];

    // Remove www from the host if it exists
    $host = preg_replace('/^www\./', '', $host);

    // Get the request URI (the part after the domain)
    $request_uri = $_SERVER['REQUEST_URI'];

    // Construct the full URL starting from the domain
    $current_url = $host . $request_uri;

    return esc_html($current_url);
}

// Register the shortcode
function register_current_page_url_shortcode() {
    add_shortcode('current_page_url', 'get_current_page_url');
}

add_action('init', 'register_current_page_url_shortcode');

Step 2: Use the Shortcode in Your Content

Now that you have registered the shortcode, you can use it in your posts, pages, or widgets to display the current page URL.

  1. Edit a Post or Page: Go to the WordPress admin dashboard and edit the post or page where you want to display the current page URL.
  2. Insert the Shortcode: Add the given below shortcode at the location where you want the URL to appear.
  3. Publish or Update: Save your changes by publishing or updating the post or page.
[current_page_url]

Step 3: View the Result

Visit the post or page where you added the shortcode. You should see the current page URL displayed at the location where you inserted the shortcode.

Example Use Cases

Example 1: Displaying the URL on a Page

Add the shortcode expertsonsite.com/wordpress-current-page-url/ to a page to show visitors the URL of the page they are on. This can be useful for sharing or reference purposes.

Example 2: Using in a Hyperlink

You can use the shortcode within a link to create dynamic URLs. For example:

<a href="[current_page_url]">Share this page</a>

This will create a hyperlink that directs users to the current page.

Example 3: Form Redirection

Redirect users back to the current page after form submission by setting the form’s action attribute to the shortcode:

<form action="[current_page_url]" method="post">
    <!-- form fields -->
    <input type="submit" value="Submit">
</form>

Conclusion

By using the current page URL shortcode in WordPress, you can enhance the functionality and user experience of your website. Whether for sharing links, tracking analytics, or displaying dynamic content, this simple yet powerful tool can be easily implemented and used across your site. Follow the steps above to add this feature to your WordPress site and start taking advantage of the current page URL shortcode today.

Amanat

Editor Information

My name is Amanat Ali, and I am an SEO expert with 8 years of experience. I am proficient in HTML, CSS, and social media marketing, with vast experience in email marketing.

Leave a Comment

× How can I help you?