Using after_setup_theme hook in WordPress

after_setup_theme is an action hook in WordPress which is called on every page after the theme initialization process is completed. Primarily, this hook is used to:

  1. Perform basic theme setup and registration.
  2. Override parent theme functionality via child themes.

Usage:

add_action( 'after_setup_theme', 'ccw_theme_setup_callback' );

function ccw_theme_setup_callback() {
	/* Add your custom hooks or theme support */
}

In the above code, ccw_theme_setup_callback is the callback function which is executed each time after_setup_theme called. You can use this function to register your custom hooks or to add theme support for a specific feature.

For example,

add_action( 'after_setup_theme', 'ccw_theme_setup_callback' );

function ccw_theme_setup_callback() {
        /* Add Theme Support */
	add_theme_support( 'post-thumbnails' );

        /* Register Navigation Menu */
        register_nav_menu( 'top', __( 'Top Menu', 'ccw-theme' );
        
        /* Register theme sidebar */
        add_action( 'widgets_init', 'ccw_register_sidebar' );
}

function ccw_register_sidebar() {
        /* Code to register your sidebar */
}

In this example, We are adding theme support for post thumbnails, registering our navigation menu and executing ccw_register_sidebar function which allow you to register a sidebar.

You can also use this hook to override or remove parent theme’s action via child theme.

add_action( 'after_setup_theme', 'ccw_remove_parent_action' );

function ccw_remove_parent_action()
{
         remove_action( 'widgets_init', 'footer_sidebar_init' );
}

The above code will remove footer_sidebar_init action after theme is loaded.

Quick Tip : How to Show Total Number of Attachments in WordPress

Leave a Comment.