get_page_link()WP 1.5.0

Gets the URL (link) of a permanent WordPress page (post type page).

Understands when the page is set as the homepage of the site: see the option page_on_front.

Instead of this function, it is almost always better to use get_permalink() — a universal function that uses this function when needed.

Used By: get_permalink()
1 time — 0.0038071 sec (very slow) | 50000 times — 7.68 sec (fast)
Hooks from the function

Returns

String. URL.

Usage

get_page_link( $post, $leavename, $sample );
$post(int|WP_Post)
ID of the page or its object.
Default: global $post
$leavename(true|false)
Whether to leave the holder %pagename% as is. true - leave the holder without replacing it with the page name. The page name is constructed from the name of the current page and its parents, for example, parent_page_name/current_page_name.
Default: false
$sample(true|false)
true — get a sample link considering the permalink structure. This may be needed when the link is forcibly returned in a simple form (not permalink - /?pagename=contacts), and we need to get the permalink (/contacts).
Default: false

Examples

0

#1 Demo

// page post type - for which this function is intended
echo get_page_link( 10124 ); // https://example.com/about/privacy-policy

// leave the placeholder
echo get_page_link( 10124, true ); // https://example.com/%pagename%

WARNING: the function is not intended to be used for post post type and other custom post types, so it may return an incorrect results.

echo get_page_link( 13564 ); // https://example.com/atributy-scrset-sizes

// the correct is
echo get_permalink( 13564 ); // https://example.com/id_13564/atributy-scrset-sizes.html
0

#2 Example of use in an HTML template

<a href="<?= esc_url( get_page_link( 40 ) ); ?>">
	<?php esc_html_e( 'Map', 'textdomain' ); ?>
</a>

Changelog

Since 1.5.0 Introduced.

get_page_link() code WP 6.9.1

function get_page_link( $post = 0, $leavename = false, $sample = false ) {
	$post = get_post( $post );

	if ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID ) {
		$link = home_url( '/' );
	} else {
		$link = _get_page_link( $post, $leavename, $sample );
	}

	/**
	 * Filters the permalink for a page.
	 *
	 * @since 1.5.0
	 *
	 * @param string $link    The page's permalink.
	 * @param int    $post_id The ID of the page.
	 * @param bool   $sample  Is it a sample permalink.
	 */
	return apply_filters( 'page_link', $link, $post->ID, $sample );
}