• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

HANDS ON WORDPRESS

Making a Living with WordPress

  • Home
  • Blog
  • About
    • About Hands On WordPress
  • The Austin WordPress Community
  • Contact
  • Show Search
Hide Search
You are here: Home / WordPress / What Are WordPress Action Hooks and Filters

What Are WordPress Action Hooks and Filters

Nick Batik · March 31, 2016 · Leave a Comment

An Introduction to WordPress Action Hooks and Filters

Nick Batik Discussing Hoooks and Filters
Nick Batik Discussing Hoooks and Filters

In this month’s meetup, Nick Batik introduces our members to the real power that lurks inside WordPress core — the ability to add, remove, and modify almost every function that WordPress performs.  It is this facility to customize that lets us transform WordPress from a simple blogging tool to sophisticated, special purpose websites.

In order to be effective at expanding the functionality of your site — creating and modifying themes, and writing plugins, you must understand hooks and filters.

Nick Batik covered the basic concepts, and help our members understand terms and phrases, and provide useful examples of how to write and use WordPress Hooks and Filters.

WPATX Meeting Notes

Action Hooks

Action hooks and filters allow you to “hook” a custom function to an existing function, which allows you to modify WordPress functionality without editing core files.

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

How to 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: http://wp.tutsplus.com/tutorials/quick-tip-add-extra-contact-methods-to-user-profiles

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).'”>’.’Continue Reading’.'</a>’;
}
add_filter(‘excerpt_more’, ‘wdc_no_more_jumping’);
Source: http://wpshout.com/wordpress-functions-php/

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))
wp_enqueue_script(‘comment-reply’);
}
}

add_action(‘get_header’, ‘enable_threaded_comments’);

Source: http://wpshout.com/wordpress-functions-php/

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”>’;
} else {
echo ‘<div id=”message”>’;
}

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 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(
// ‘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 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: http://snipplr.com/view.php?codeview&id=56180

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: http://skyje.com/2011/05/wordpress-code-snippets/

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: http://www.wprecipes.com/wordpress-hack-remove-autolinks-in-comments

Filed Under: WordPress Tagged With: Austin WordPress Meetup, WordPress Development, WordPress Hooks & Filters

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Sandi Batik

About Sandi Batik

Introverted Freelancer, WordPress trainer, consultant, curricula developer, author, unapologetic geek, unrepentant capitalist, lucky enough to do what I love … more about me about About Sandi Batik

  • Twitter

Search

Introverted Freelancer

Traits of Successful Introverted Freelancers

Traits of Successful Introverted Freelancers

2017 Business Check-up Workshop

2017 Business Check-up Workshop

Expanding Your Business With Automated Marketing Funnels

Expanding Your Business With Automated Marketing Funnels

How to Use Permission Marketing to Build Your WordPress Business

How to Use Permission Marketing to Build Your WordPress Business

How Much Should I Charge for Building or Designing a WordPress Website?

How Much Should I Charge for Building or Designing a WordPress Website?

Project Management

Keeping Scope Creep From Killing Your Schedule and Profit Margin

Keeping Scope Creep From Killing Your Schedule and Profit Margin

Project Management for WordPress Freelancers

Project Management for WordPress Freelancers

WordPress

Securing and Maintaining Your WordPress Site

Securing and Maintaining Your WordPress Site

How The WordPress Media Library Works — 2018

How The WordPress Media Library Works — 2018

How To Build an Information Structure for Your WordPress Site

How To Build an Information Structure for Your WordPress Site

How WordPress Themes Really Work

How WordPress Themes Really Work

How to Create and Manage eMail Newsletters from Your WordPress Site

How to Create and Manage eMail Newsletters from Your WordPress Site

How to Secure and Maintain Your WordPress Site

How to Secure and Maintain Your WordPress Site

Copyright © 2010-2023 Hands On WordPress · All Rights Reserved