Could we help you? Please click the banners. We are young and desperately need the money
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.
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.
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".
"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.
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!';
}
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;
}
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>';
}
WordPress offers native ways to check the current page too. These include:
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.
$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.
Here’s when our "is_current_page()" function is better:
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;
}
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.
While testing, output current URLs to ensure correct matches:
echo 'Current URL: ' . $_SERVER['REQUEST_URI'];
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.