Extending theme features through available filters and actions

We already discussed the importance of using child themes to customize and extend theme templates. Using the WordPress filter and action hooks is another way of customizing theme features. We will mainly use these hooks to extend the functions provided by the theme. However, we can use some of the filter and action hooks to change parts of templates without needing to replace them completely.

In WordPress, an action is a PHP function that is executed at specific points in a process. A filter is a function that is used to modify the existing data or features in a process. Even though actions are intended to do something before or after an event, they are used frequently to add content to templates. More about actions and filters will be covered in upcoming chapters.

Once we choose a theme, we can explore the actions and filters within the theme to understand its extendable features and locations. The simplest way to identify these actions and filters is to use the directory or project search feature in your code editor. Most quality code editors provide this feature by default. You should use the term apply_filters to search for filters in the theme, and do_action to find out the actions defined in it. You may find a small number of these hooks in free themes and hundreds in premium themes. These hooks consist of built-in WordPress hooks as well as theme-specific hooks. As a developer, you need to have a thorough knowledge of the common built-in WordPress hooks in themes. Let's take a look at some of the common filters and actions used in themes:

add_filter( 'the_content', 'wpquickstart_add_custom_text_single_posts' );  
function wpquickstart_add_custom_text_single_posts( $content ) { 
    if ( is_single() && in_the_loop() && is_main_query() ) { 
        return $content . "Custom Text After Post"; 
    }  
    return $content; 
} 
function wpquickstart_remove_website_field($fields) { 
    unset($fields['url']); 
    return $fields; 
}  
add_filter('comment_form_default_fields', 'wpquickstart_remove_website_field'); 
add_action( 'wp_nav_menu_item_custom_fields', 'wpquickstart_menu_button_fields', 10, 4 ); 
function wpquickstart_menu_button_fields( $item_id, $item, $depth, $args ) {     ?> 
  <div class="fusion-menu-options-container"> 
   <div class="option-details"> 
     <h3><?php _e( 'Menu Class', 'wpquickstart' ); ?></h3> 
     <p class="description"><?php _e( 'Add custom css class.', 'wpquickstart' ); ?></p> 
   </div> 
   <div class=""> 
     <input type="text" id="wpquick-menu-class" name="wpquick-menu-class" value="" /> 
   </div> 
  </div> 
<?php } ?> 

These are some of the frequently used ones, among hundreds of possible WordPress actions and filters. You should explore these hooks by using various WordPress themes in order to identify the necessary ones for your implementations.

The filters and actions discussed in the preceding section are used for common WordPress purposes and hence can be used in any theme. Theme-specific actions and filters, on the other hand, are designed for advanced theme features. Most of these actions and filters cannot be used in other themes. We can easily identify theme-specific hooks by looking at the prefix used for hook names. The Twenty Seventeen theme uses twentyseventeen_ as the prefix for theme-specific hooks. Let's consider one of the filters that's available in the Twenty Seventeen theme to understand the use of theme-specific hooks:

apply_filters( 'twentyseventeen_content_width', $content_width ); 

This filter is used to customize the width of the page based on page layout. Let's see how we can implement this filter to modify the width:

add_filter( 'twentyseventeen_content_width', 'wpquickstart_content_width' ); 
function wpquickstart_content_width($content_width){ 
  if(is_page()) { 
    $content_width = "800"; 
  } 
  return $content_width; 
} 

In the preceding code, we changed the width of pages in the Twenty Seventeen theme by using the theme-specific filter. We can add more conditions to change the width and display different types of layouts.