Using WordPress ‘get_page_uri’ PHP filter

The get_page_uri WordPress PHP filter allows you to modify the URI of a specific page.

Usage

add_filter('get_page_uri', 'your_custom_function', 10, 2);

function your_custom_function($uri, $page) {
    // your custom code here
    return $uri;
}

Parameters

  • $uri (string) – The page URI that you want to modify.
  • $page (WP_Post) – The page object containing information about the specific page.

More information

See WordPress Developer Resources: get_page_uri

Examples

Change page URI to include parent page slug

This example modifies the page URI to include the parent page slug for better organization.

add_filter('get_page_uri', 'include_parent_page_slug', 10, 2);

function include_parent_page_slug($uri, $page) {
    if ($page->post_parent) {
        $parent_slug = get_post_field('post_name', $page->post_parent);
        $uri = $parent_slug . '/' . $uri;
    }
    return $uri;
}

Add a custom prefix to page URI

This example adds a custom prefix to the page URI.

add_filter('get_page_uri', 'add_custom_prefix', 10, 2);

function add_custom_prefix($uri, $page) {
    $prefix = 'custom-prefix/';
    $uri = $prefix . $uri;
    return $uri;
}

Replace hyphens with underscores in page URI

This example replaces hyphens with underscores in the page URI.

add_filter('get_page_uri', 'replace_hyphens_with_underscores', 10, 2);

function replace_hyphens_with_underscores($uri, $page) {
    $uri = str_replace('-', '_', $uri);
    return $uri;
}

Add a query string to the page URI

This example adds a custom query string to the page URI.

add_filter('get_page_uri', 'add_query_string', 10, 2);

function add_query_string($uri, $page) {
    $query_string = '?custom_key=custom_value';
    $uri .= $query_string;
    return $uri;
}

Remove numbers from the page URI

This example removes any numbers present in the page URI.

add_filter('get_page_uri', 'remove_numbers_from_uri', 10, 2);

function remove_numbers_from_uri($uri, $page) {
    $uri = preg_replace('/[0-9]/', '', $uri);
    return $uri;
}