Super useful WordPress action hooks and filters

Action hooks and filters are very useful in WordPress. They allow you to "hook" a custom function to an existing function, which allows you to modify WordPress functionality without editing core files. Today, here are 10 super usefull action hooks and filters to supercharge your WordPress install!

Prevent automatic image compression

By default, WordPress compress jpg images when you upload them to your blog. This is useful because it saves bandwidth and loading time, but sometimes you may prefer to have full quality images (For example, if you’re a photographer using WordPress to showcase your work).

Paste the code below into your functions.php file to remove automatic compression of images.

add_filter('jpeg_quality', function($arg){return 100;});

→ Source:

Add target=”blank” to all links

I’ve never been a fan of target="blank" links, but I’m always surprised to see how clients love them. So if you need to transform all links to target="blank" links, here is an easy solution.

This function have to be pasted in your functions.php file.

function autoblank($text) {
	$return = str_replace('<a', '<a target="_blank"', $text);
	return $return;
add_filter('the_content', 'autoblank');

→ Source:

Add extra contact methods to user profiles

By default, WordPress allow users to enter an AIM name on their profile, but no Facebook and no Twitter names! But in 2012, those websites are far more popular than the good old AIM (ah, memories…).

In order to add more contact methods to user profile, simply paste this hook in your functions.php file. In this example it will add Facebook and Twitter, but it can be used for any website or service you need.

function my_user_contactmethods($user_contactmethods){
  $user_contactmethods['twitter'] = 'Twitter Username';
  $user_contactmethods['facebook'] = 'Facebook Username';

  return $user_contactmethods;

add_filter('user_contactmethods', 'my_user_contactmethods');

→ Source:

Remove the “read more” jump

On WordPress blogs, when you click on a “Read more” link, it automatically drops to the point in the article you theoretically just got to the end of. If you don’t really like that jump, simply paste the following code into your functions.php file to get rid of it.

function wdc_no_more_jumping($post) {
     return '<a href="'.get_permalink($post->ID).'" class="read-more">'.'Continue Reading'.'</a>';
add_filter('excerpt_more', 'wdc_no_more_jumping');

→ Source:

Automatically enable threaded comments

By default, WordPress do not enable threaded comments. If you want/need to change this, here is a handy code snippet to paste in your functions.php file:

function enable_threaded_comments(){
 if (!is_admin()) {
  if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))

add_action('get_header', 'enable_threaded_comments');

→ Source:

How to show an urgent message in the WordPress admin area

When writing custom WordPress theme or plugins, you might want to inform users that something important needs doing, perhaps due to an upgrade. e.g. You need the user to update a setting, or check that their settings have been transposed correctly. Here is a ready to use hook to display a custom message to admins.

function showMessage($message, $errormsg = false){
	if ($errormsg) {
		echo '<div id="message" class="error">';
	} else {
		echo '<div id="message" class="updated fade">';

	echo "<p><strong>$message</strong></p></div>";

function showAdminMessages() {
    showMessage("You need to upgrade your database as soon as possible...", true);

    if (user_can('manage_options') {
       showMessage("Hello admins!");

add_action('admin_notices', 'showAdminMessages'); 

→ Source:

Automatically replace words in your posts

Imagine this: your blog was named “myblog” and for some reason you renamed it “mysuperblog”. Don’t edit your XXX posts to replace every single occurence! Instead, paste this useful hook into your functions.php file and let it do the work for you.

function replace_text_wps($text){
    $replace = array(
        'wordpress' => '<a href="#">wordpress</a>',
        'excerpt' => '<a href="#">excerpt</a>',
        'function' => '<a href="#">function</a>'
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;

add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');

→ Source:

Add post thumbnails to RSS feed

This very cool piece of code will get the post thumbnail and automatically add it to your RSS feeds. Paste the code into functions.php and save the file. Don’t forget that you need to use a theme that supports post thumbnails for this snippet to work.

function cwc_rss_post_thumbnail($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID) .
        '</p>' . get_the_content();

    return $content;
add_filter('the_excerpt_rss', 'cwc_rss_post_thumbnail');
add_filter('the_content_feed', 'cwc_rss_post_thumbnail');

→ Source:

Quick maintenance mode

Sometimes, you need to put your blog on hold while performing some maintenance. Many plugins are allowing you to do so, but here is a simpler solution: Just paste the following snippet into your functions.php file and save it. Your blog is now unavailable to anyone except administrators. Don’t forget to remove the code when you’re done with maintenance!

function cwc_maintenance_mode() {
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
        wp_die('Maintenance, please come back soon.');
add_action('get_header', 'cwc_maintenance_mode');

→ Source:

Remove comments autolinks

If someone leaves a comment containing a url on your WordPress blog, the url will be automatically transformed to a link by WordPress. This can be useful, but personally I don’t like to see many links in comments, especially when they’re a bit spammy.
Removing autolinks is pretty easy: Just paste the following code into your functions.php file: Once you saved the file, you’ll notice that autolinks have disappeared.

remove_filter('comment_text', 'make_clickable', 9);

→ Source:

By the way, I just created a Facebook page for Cats Who Code, some feel free to “like” the blog!

  • xander

    Thank you! Haven’t seen those before.

  • You do not want to add “_blank” to all your links unless you want to lose those readers who want to decide themselves if they want to open a new tab or not. Oh no, wait, you won’t lose me because I strip all “_blank” and “_new” targets with an add-on 😉

  • Really useful ones…

  • Thank you, man!
    I will translate it to my own blog on russian. Backlink to you 😉

  • Really useful post! Bookmarked!

  • Good edition, i will share it on my blog.

  • “Quick maintenance mode” or you can add a code to the .htaccess and enable from your own you so you can do the maintainace.

  • Ha! Sorry, I got hung up on the ‘target = “blank” ‘ tip. This is my favorite to use in my web pages (since it lets visitors explore external links yet keeps my page open in their browser) and my favorite to find on other web pages )since it lets me go off on a surfing tangent but not lose sight of the page I was really interested in).

    Great post – and I did read the rest of the tips 🙂

  • Bill Woodland

    The target attribute has been deprecated. Even so, it’s target=”_blank”, not target=”blank” to get it to open in a new window. The opening of a link in the current window, a new window or a new tab is a behavior. Behavior should be done with javascript.

    Still, NEVER EVER EVER control how a link opens up for the user. If I want it in a new tab, I control-click or click with the scroll wheel. If I want it in a new window, I shift-click. People who don’t know these tricks DO know how to use the back button to get back to your site.

    If the link is actuated by a javascript onclick, then I’m going to get really angry with you, and probably send you a scathing email about how links should be handled by the browser,

    If you insist on controlling your users, you will eventually lose them.

    @rheo just because you left your page open in another window doesn’t mean that your user actually wanted to go back to your site. In that case, you are forcing your user to close a window they didn’t need or want. Leave it up to your user.

  • Wow! Interesting, I’ll try this. Will this be applicable to all WordPress themes? Thanks for sharing.

  • I have a question , that we write a function in function.php. then how to enable function or disable function from wp-admin.. so we have not to touched anything from function.php .

    also by this admin have power to choose function which may be run on his choice.

  • Thanks so much for sharing this and saving my time. I was looking for some way to replace html in a post automatically and the “Add target=”blank” to all links” tip worked fine for me!!

  • Thanks, this is just what I was searching for. Bookmarked 😉

  • sergio

    Web Designer em Mongaguá, Praia Grande, Santos e Litoral