get_page_uri() WordPress Function

The get_page_uri() function in WordPress is used to return the URI for a given page. This is useful for creating links to pages, or for use in permalinks.

get_page_uri( WP_Post|object|int $page ) #

Build the URI path for a page.


Description

Sub pages will be in the "directory" under the parent page post name.


Top ↑

Parameters

$page

(WP_Post|object|int)(Optional) Page ID or WP_Post object. Default is global $post.


Top ↑

Return

(string|false) Page URI, false on error.


Top ↑

More Information

If the page has parents, those are prepended to the URI to provide a full path. For example, a third level page might return a URI like this:

top-level-page/sub-page/current-page

This function will return a “slug” style URI regardless of whether “pretty” Permalinks are configured.


Top ↑

Source

File: wp-includes/post.php

function get_page_uri( $page = 0 ) {
	if ( ! $page instanceof WP_Post ) {
		$page = get_post( $page );
	}

	if ( ! $page ) {
		return false;
	}

	$uri = $page->post_name;

	foreach ( $page->ancestors as $parent ) {
		$parent = get_post( $parent );
		if ( $parent && $parent->post_name ) {
			$uri = $parent->post_name . '/' . $uri;
		}
	}

	/**
	 * Filters the URI for a page.
	 *
	 * @since 4.4.0
	 *
	 * @param string  $uri  Page URI.
	 * @param WP_Post $page Page object.
	 */
	return apply_filters( 'get_page_uri', $uri, $page );
}


Top ↑

Changelog

Changelog
VersionDescription
4.6.0The $page parameter was made optional.
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More