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:
- string
$linkthe page link - int
$post_idthe page id - bool
$samplewhether 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.