get_page_link

Retrieves the permalink for the current page or page ID.

Description

(string) get_page_link( (constant) $post = false, (constant) $leavename = false, (bool) $sample = false ); 

Respects page_on_front. Use this one.

Returns (string)

The page permalink.

Parameters (3)

0. $post — Optional. (constant) => false
Post ID or object. Default uses the global $post..
1. $leavename — Optional. (constant) => false
Whether to keep the page name. Default false.
2. $sample — Optional. (bool) => false
Whether it should be treated as a sample permalink. Default false.

Usage

  1. if ( !function_exists( 'get_page_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Post ID or object. Default uses the global `$post`. 
  5. $post = false; 
  6.  
  7. // Optional. Whether to keep the page name. Default false. 
  8. $leavename = false; 
  9.  
  10. // Optional. Whether it should be treated as a sample permalink. 
  11. // Default false. 
  12. $sample = false; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_page_link($post, $leavename, $sample); 
  16.  

Defined (2)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_page_link( $post = false, $leavename = false, $sample = false ) { 
  2. $post = get_post( $post ); 
  3.  
  4. if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) ) 
  5. $link = home_url('/'); 
  6. else 
  7. $link = _get_page_link( $post, $leavename, $sample ); 
  8.  
  9. /** 
  10. * Filters the permalink for a page. 
  11. * @since 1.5.0 
  12. * @param string $link The page's permalink. 
  13. * @param int $post_id The ID of the page. 
  14. * @param bool $sample Is it a sample permalink. 
  15. */ 
  16. return apply_filters( 'page_link', $link, $post->ID, $sample ); 
  1. function _get_page_link( $post = false, $leavename = false, $sample = false ) { 
  2. global $wp_rewrite; 
  3.  
  4. $post = get_post( $post ); 
  5.  
  6. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); 
  7.  
  8. $link = $wp_rewrite->get_page_permastruct(); 
  9.  
  10. if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) { 
  11. if ( ! $leavename ) { 
  12. $link = str_replace('%pagename%', get_page_uri( $post ), $link); 
  13.  
  14. $link = home_url($link); 
  15. $link = user_trailingslashit($link, 'page'); 
  16. } else { 
  17. $link = home_url( '?page_id=' . $post->ID ); 
  18.  
  19. /** 
  20. * Filters the permalink for a non-page_on_front page. 
  21. * @since 2.1.0 
  22. * @param string $link The page's permalink. 
  23. * @param int $post_id The ID of the page. 
  24. */ 
  25. return apply_filters( '_get_page_link', $link, $post->ID );