Getting the URI of a page in WordPress using get_page_uri

The get_page_uri function in WordPress is a function that retrieves the URI of a page. The URI, or Uniform Resource Identifier, is a string of characters that identifies a name or a resource on the Internet. This function can be used to find the URI for any page within a WordPress site.

One of the main uses of the get_page_uri function is to generate links to specific pages. By using this function, one can programmatically create links to any page within the WordPress site, without having to know the exact URL of the page. This can be useful in a variety of situations, such as creating navigation menus, generating lists of related pages, or building custom page templates.

Another use of the get_page_uri function is for SEO purposes. By using this function, one can ensure that the correct, canonical URL is used for each page, which can help to improve the site’s search engine rankings.

Parameters Accepted by the get_page_uri Function

The get_page_uri function in WordPress accepts a single parameter:

  • $page(WP_Postobjectint) – This is an optional parameter. It can either be a Page ID or a WP_Post object. If no value is provided, the function will use the global $post by default.

Return Value of the get_page_uri Function

The get_page_uri function returns a string that represents the Page URI. If an error occurs during the execution of the function, it will return false.

If the function does not accept any parameters, it will simply use the global $post as its default value.

Examples

How to get the URI of a specific page by ID

$page_id = 123; // Replace with your page ID
$page_uri = get_page_uri($page_id);
echo '<p>Page URI: ' . $page_uri . '</p>';

In this example, we’re using the get_page_uri() function to get the URI of a specific page. We first specify the ID of the page we’re interested in, then we pass that ID to the function. The result is echoed out in a paragraph.

How to check if a page URI exists

$page_id = 123; // Replace with your page ID
$page_uri = get_page_uri($page_id);
if ($page_uri) {
 echo '<p>Page URI: ' . $page_uri . '</p>';
} else {
 echo '<p>No page found with this ID.</p>';
}

In this example, we’re still using the get_page_uri() function to get the URI of a specific page. But this time, we’re also checking if the URI actually exists. If it does, we echo it out. If it doesn’t, we echo out a message saying no page was found with that ID.

How to get the URI of the current page

global $post;
$page_uri = get_page_uri($post->ID);
echo '<p>Current Page URI: ' . $page_uri . '</p>';

This time, we’re using the get_page_uri() function to get the URI of the current page. We access the global $post object and use its ID as the parameter for our function. The result is then echoed out in a paragraph.

Conclusion

The get_page_uri function in WordPress is an effective feature that enables the retrieval of the URI path for any given page within a WordPress website. It is particularly useful in situations where developers need to programmatically generate links or references to specific pages. The function works by taking the ID of a page as an input and returning the URI path as a string. This function can be leveraged in a variety of ways, such as in the creation of dynamic navigation menus, breadcrumb trails, or even in custom URL rewriting rules.

Related WordPress Functions