#16: page_link

The page_link filter allows you to do change the permalink returned for the current page when using get_permalink(). When get_permalink() is used with pages, it employs get_page_link(), which respects the show_on_front and page_on_front options. This means that if the current page is set as your front page, the permalink returned will be the home_url().

The filter is evaluated in get_page_link() in the wp-includes/link-template.php file.

page_link accepts 3 arguments:

  1. string $link the page link
  2. int $post_id the page id
  3. bool $sample whether or not to return a ‘sample’ permalink

Example:

Let’s say that for some reason, you have a page set as your front page but you want to return the page’s actual permalink when using get_permalink(). The following checks the show_on_front and page_on_front options to see if there’s a match, and if there is, returns the actual permalink instead.

Note: this has the possibility of wreaking havoc on how WordPress determines a ‘Home’ link in menus as well as how the front page may be determined.

View the code example on Gist.

Have a suggested improvement? You can fork the Gist and comment back with the link. If all is agreeable, I’ll merge in your changes. Crowd-sourced documentation FTW!

2 thoughts on “#16: page_link

Leave a Reply

Your email address will not be published. Required fields are marked *