#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.

#15: the_content

The the_content filter makes it possible to modify the content of a post, page or other post type. It is probably one of the most commonly-used — and misused — filter hooks in the WordPress code base.

Misuse of the_content is rampant largely due to the geography of a post’s content on the page. Developers want something to show up at the end of the post so sometimes they’ll use this hook to accomplish that placement. Unfortunately, whatever it is they’re hooking onto the beginning or end of the_content often has little or nothing to do with the post itself.

It’s one of those filters where you can look to ‘how Core does it’ to really see its intended purpose. Out of the box, Core filters the_content in about 10 different places. This is to accomplish much of the behind-the-scenes post cleanup users never really see, which include:

  • oEmbed handling
  • converting smilies to images
  • removing unwanted characters
  • auto-adding paragraph tags
  • processing shortcodes
  • and more

In all such cases, Core filters the post content itself, but doesn’t tack on unrelated stuff.

the_content is evaluated in:

It accepts a single argument, a string containing the post’s content.

Example:

Let’s say you’re one of those people who just can’t get out of the habit of using two spaces after periods and you’ve been writing on your blog for years. The following will filter the_content to convert those pesky double spaces to singles.

View the code example on Gist.

#14: the_editor_content

The the_editor_content filter makes it possible for you to pre-fill the content editor in the post editing screen.

It is evaluated in the _WP_Editors::editor() method in wp-includes/class-wp-editor.php. The _WP_Editors class is instantiated via wp_editor() located in wp-includes/general-template.php.

the_editor_content‘s single argument is the editor content string, empty by default.

Note: to avoid a situation where your content ends up duplicating every time you update the post, you should check if the editor content is empty before returning the filtered version of it. We’ll cover this in the example.

Example:

Let’s say you run a site that documents a lot of very similar things, like say, filters. It can be kind of a drag to write out and/or copy/paste the same template into the editor over and over, so the following will show you how to inject a post template into the editor prior to the first save.

View the code example on Gist.