Wordpress: How to hook everything?

Today, we’ll find out what exactly WordPress hooks are and how they can help you on your way you become to WordPress expert.

What WordPress hooks can do?

As you known, WordPress hooks allow users to modify a WordPress theme or plugin, or add their own code to various parts of WordPress without modifying the original files. WordPress provides us add_action and add_filter to modify anywhere we want in the plugin or in the theme, where put apply_filters or do_action


Action Hooks

Action hooks are put in the WordPress core, theme and plugin code where  we can insert additional code and customise the code to do additional functions that we want. For example: A commonly used action hook wp_head used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else before the </head> tags.

To hook on to an action, create a functions.php file in your theme’s  and hook it by using the add_action() function, as follows:

add_action( 'wp_head', 'add_meta_description' );

function add_meta_description () {

    echo '<meta name="description" content="Your description" />' ;

    }

 

Duplicate content is an SEO problem for many websites so we can use the new rel="canonical" attribute to prevent duplicate content in paged comments with the code below.

function canonical_for_comments() {
  global $cpage, $post;
  if ( $cpage > 1 ) :
    echo "n";
      echo "<link rel='canonical' href='";
      echo get_permalink( $post->ID );
      echo "' />n";
   endif;
}

add_action( 'wp_head', 'canonical_for_comments' );

 

Filter Hooks

Filter hooks are used to manipulate output. Filter hooks canbe used for truncating text, changing formatting of content. Custom code is added as a filter using the add_filter() function. 

  • If you want to hide admin menu bar, you can use the snippet below
add_filter('show_admin_bar', '__return_false');​

     

  • Using the snippet below if you want to add custom class to Contact Form 7 form

 

add_filter( 'wpcf7_form_class_attr', 'custom_form_class_attr' );

function custom_form_class_attr( $class ) {
    $class .= ' your-class';
    return $class;
}
  • To remove wpautop from shortcode content, we use this snippet

 

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99 );
add_filter( 'the_content', 'shortcode_unautop', 100 );

 

Reference:
Action hooks: http://codex.wordpress.org/Plugin_API#Actions
Filter hooks: http://codex.wordpress.org/Plugin_API#Filters

 

Hopefully, you now fully understand how the Wordpress hooks work. In next article I will show you how to create your own hooks. Please feel free to ask me anything you want in this article.

 

 

macanhhuy
macanhhuy Hire Me

PHP, Python, Ruby, Java Programmer - Website Design Developer

I'm good at CSS3, HTML5, Javascript (NodeJS, AngularJS 2.0, Ionic 2, KnockoutJS, Backbone, EmberJS, jQuery, ES6, Ionic framework), MEAN, Ruby (Ruby on Rails), Python(Django framework, Web2py framework), PHP (Wordpress, Symfony 2, Laravel 5, Opencart, Magento, CakePHP) , Java (Spring, Strut, Hibernate, Jive SBS), Objective C, Swift (iOS) . I have worked with all of them for four years and have al...

Hire Me

Next Article

Arabic Translation