WordPress action hooks and filters: practical examples

WordPress action hooks and filters: practical examples

Action hooks and filters are very useful in WordPress. They allow you to "hook" a custom function to an existing function and modify WordPress functionality without editing core files. Here are 10 practical examples of WordPress hooks and filters.


Prevent automatic image compression

By default, WordPress compresses jpg images when you upload them onto 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: http://www.wprecipes.com/prevent-wordpress-to-compress-your-jpg-images

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 has 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: http://www.catswhocode.com/blog/snippets/add-target_blank-on-all-link

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: http://wpshout.com/wordpress-functions-php/

How to show an urgent message in the WordPress admin area

When writing custom WordPress themes or plugins, you might want to inform users that something important needs to be done, 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: http://www.wpdoctors.co.uk

Looking for WordPress hosting? Get 50% off HostGator WP plans with coupon catswhocode!

Automatically replace words in your posts

Imagine this: your blog was named “myblog” and for some reason you renamed it “mysuperblog”. Don’t manually edit your 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(
        // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
        '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: http://wpsnipp.com/

Add custom page navigation to WooCommerce

WooCommerce, like WordPress, doesn’t use page navigation by default. This can be changed easily on WordPress with the WP PageNavi plugin. When using WooCommerce, it requires a little hack to work.

The first thing to do is to install the WP PageNavi plugin. Once done, simply open your functions.php file and paste the following code in it.

remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
		wp_pagenavi(); 		
	}
add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);

Source: WP Snacks

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 the 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');

Remove the URL field from the comment form

The default comment system receives very often a lot of spam. If you’re having this problem on your site, the following filter can be a good help in order to prevent spam.

function remove_comment_fields($fields) {
    unset($fields['url']);
    return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');

Source: http://www.wprecipes.com/how-to-remove-the-url-field-from-wordpress-comment-form/

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’ve saved the file, you’ll notice that autolinks have disappeared.

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

Source: http://www.wprecipes.com/wordpress-hack-remove-autolinks-in-comments

Any other WordPress hooks or filters you find really useful? Let me know in a comment!

Enjoyed this article? Please consider donating: 3FVR5qaxYV8yAgNUUS7FC3o8c54YfDR5zK
  • hi,

    thanks for this nice posting , i am appreciate your knowledge your gathering so informative information .