Menü schliessen
Created: May 9th 2025
Last updated: May 16th 2025
Categories: Php,  Wordpress
Author: Ian Walser

How to Check If a User Is on a Specific Page in WordPress

Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Why You Might Want to Detect the Current Page in WordPress

Whether you're building a plugin, customizing a theme, or adding dynamic functionality, sometimes you need to know exactly what page a user is viewing. WordPress offers many methods to do this, but depending on your use case—like when working with raw PHP—having a lightweight, reliable way to determine the current page can save you time and performance overhead.

The Custom Function to Detect the Current Page

Here’s a simple PHP function that checks if the current user is on a specific page based on the URL:

function is_current_page($page)
{
    $url = $_SERVER['REQUEST_URI'];

    if (str_ends_with($page, $url) !== false) {
        return true;
    }

    return false;
}

This function compares the current URL path with a target page URL string. If the current URL matches the end of the target page string, the function returns true, confirming the user is on that page.

How It Works: Breaking Down the Code

$_SERVER['REQUEST_URI']: Getting the Current Page Path

This global variable holds the URI that was given in order to access the page. For example, visiting "https://example.com/contact" sets "$_SERVER['REQUEST_URI']" to "/contact".

Using "str_ends_with()" to Compare URLs

"str_ends_with()" is a PHP 8+ function that checks if one string ends with another. It's ideal for determining if a URL path ends with a given string—just what we need to see if the user is on a certain page.

Return Value

If there's a match, the function returns true; otherwise, it returns false. This allows you to wrap page-specific logic easily:

if (is_current_page('/contact')) {
    echo 'You are on the contact page!';
}

Using the Function in WordPress Templates

1. Add the Function to "functions.php"

Place the "is_current_page()" function inside your theme’s "functions.php" file:

// functions.php
function is_current_page($page)
{
    $url = $_SERVER['REQUEST_URI'];

    if (str_ends_with($page, $url) !== false) {
        return true;
    }

    return false;
}

2. Call the Function in Your Template

Inside template files like "header.php", "footer.php", or a custom template, you can now use the function like so:

if (is_current_page('/about')) {
    echo '<div class="special-banner">Welcome to the About Page!</div>';
}

Real-World Use Cases

  • Display a custom banner only on specific pages
  • Load scripts conditionally to improve performance
  • Style elements differently based on the page
  • Trigger tracking or analytics code on specific pages

WordPress Alternatives to Consider

WordPress offers native ways to check the current page too. These include:

Using "is_page()"

if (is_page('contact')) {
    // Do something for the contact page
}

Limitation: This method doesn’t work outside "The Loop" or in raw PHP-only environments where WordPress functions may not be available.

Using "get_queried_object()"

$page = get_queried_object();
if ($page && $page->post_name === 'services') {
    // Do something for the services page
}

Again, great within WordPress template hierarchy but not ideal for standalone logic or highly customized use cases.

Why Use the Custom Function Instead?

Here’s when our "is_current_page()" function is better:

  • Running logic outside of The Loop
  • Working inside custom PHP scripts loaded by WordPress
  • Ensuring compatibility even when other WordPress functions are unavailable
  • Creating lightweight themes/plugins with minimal dependencies

Tips for Safer and More Accurate URL Matching

1. Normalize URLs

Ensure slashes and query strings don’t affect your match:

$page = rtrim($page, '/');
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$url = rtrim($url, '/');

if ($page === $url) {
    return true;
}

2. Always Sanitize Input

If you're accepting dynamic inputs for URLs (e.g., from query strings), always sanitize them with "filter_var()" or similar to avoid injection attacks.

3. Add Debugging Outputs

While testing, output current URLs to ensure correct matches:

echo 'Current URL: ' . $_SERVER['REQUEST_URI'];

Conclusion

Knowing how to check whether a user is on a specific WordPress page can be incredibly useful, whether you're customizing functionality, optimizing performance, or enhancing user experience. While WordPress provides its own native tools, sometimes a pure PHP solution like "is_current_page()" is more flexible and better suited for advanced or low-level development.

Feel free to customize and extend this function to match your project needs. With careful implementation, this simple utility can become one of the most powerful tools in your WordPress development toolkit.