WordPress Action Hooks and Filters

DZone 's Guide to

WordPress Action Hooks and Filters

Here at DZone, we love bloggers! And devs! If you host a site with WordPress, read on for some code to help opitmize your blog.

· Web Dev Zone ·
Free Resource

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

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 occurrence! 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: 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() {
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 very often receives 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) {
    return $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.phpfile. 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!

php, web dev, wordpress development

Published at DZone with permission of Jean-Baptiste Jung , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}