add_action hooks in WordPress

WordPress hooks change how the WordPress behaves without needing to edit any PHP source files.

Here I will provide simple working example for the add_action hook filter in WordPress:

function hello_header() {
  echo "I'm in the header!";
add_action('wp_head', 'hello_header');

The result adding this code to the functions.php file of my theme would be echoing “I’m in the header!” string.

Here wp_head represents the moment before the “head” section for the page is ready. Some other moments you can use are:

  • init — moment when creating a page, both front end and administration
  • admin_init — the first thing done on every administration page
  • admin_head — the last moment in the administration page’s “head” section
  • admin_menu — constructing the navigation menu in the administration pages
  • template_redirect — occurs before the theme template file bas been chosen
  • wp_enqueue_scripts — moment to eliminate duplicate scripts
  • wp_enqueue_style — moment to eliminate duplicate styles
  • widgets_init — moment for constructing the list of active widgets
  • loop_start and loop_end — surrounding the Loop
  • wp_footer — moment to insert additional stuff before closing the body tag

And here is the complete WordPress hooks list. And here is the add_action hooks list.

How about adding meta keywords to our post via a hook? How do we get meta keywords from the post tags and categories.

Here is the code I used:

function add_metatags()
 if (is_single()){
 // posttags takes all post tags
 $posttags = get_the_tags();
    if ($posttags){
       foreach($posttags as $tag){
          $keywords[] = strtolower($tag->name);

 // now adding post categories
 foreach((get_the_category()) as $category){
    $keywords[] = strtolower($category->cat_name);
 $list = implode(", ", array_unique($keywords));
 echo '';

add_action('wp_head', 'add_metatags');

Note, apart from the add_action hooks there are also add_filter hooks described here.

tags: add_action & category: wordpress