after_setup_theme
Fires on every page load immediately after the theme is initialized. It is normally used to set up basic theme features; see add_theme_support().
This is one of the earliest hooks. It fires immediately before WordPress initialization and before the init hook.
Unlike init, WordPress has not yet determined whether the user is logged in when this hook fires.
Usage
add_action( 'after_setup_theme', 'wp_kama_after_setup_theme_action' );
/**
* Function for `after_setup_theme` action-hook.
*
* @return void
*/
function wp_kama_after_setup_theme_action(){
// action...
}
Examples
#1 after_setup_theme usage in the default Twenty Twelve theme
This example shows how to use after_setup_theme to register theme features such as featured images, post formats, and a header image.
/** Tell WordPress to run twentytwelve_setup() on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'twentytwelve_setup' );
// Set default theme options and add support for WordPress features.
function twentytwelve_setup() {
// Support an editor stylesheet. Place editor-style.css in the theme directory.
add_editor_style();
// Support post formats.
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
// Support featured images.
add_theme_support( 'post-thumbnails' );
// Add post and comment RSS links to the head section.
add_theme_support( 'automatic-feed-links' );
// Make the theme available for translation.
// Translation files must be located in /languages/.
load_theme_textdomain( 'twentytwelve', get_template_directory() . '/languages' );
$locale = get_locale();
$locale_file = get_template_directory() . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
// Support wp_nav_menu() navigation menus.
register_nav_menus( array(
'primary' => __( 'Primary Navigation', 'twentytwelve' ),
'Secondary' => __( 'Secondary Navigation', 'twentytwelve' ),
) );
// Support a custom background.
add_custom_background();
// Set and modify options in the theme's HEADER section.
if ( ! defined( 'HEADER_TEXTCOLOR' ) )
define( 'HEADER_TEXTCOLOR', '' );
// Specify a header image URL. %s is replaced with the theme directory URL.
if ( ! defined( 'HEADER_IMAGE' ) )
define( 'HEADER_IMAGE', '%s/images/headers/path.jpg' );
// Custom header height and width. These can be changed with the theme hooks shown below.
define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentytwelve_header_image_width', 940 ) );
define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentytwelve_header_image_height', 198 ) );
// Use the post thumbnail as the header image for pages and posts.
// Required dimensions: 940 pixels wide and 198 pixels high.
// Large images are reduced automatically; small images remain unchanged.
set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
// No text is needed inside the header image.
if ( ! defined( 'NO_HEADER_TEXT' ) )
define( 'NO_HEADER_TEXT', true );
// Allow header image styles to be changed from the admin area.
// add_custom_image_header( '', 'twentytwelve_admin_header_style' );
}Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
In file: /wp-settings.php
after_setup_theme
wp-settings.php 757
do_action( 'after_setup_theme' );
Where the hook is used in WordPress
wp-includes/class-wp-customize-manager.php 576
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
wp-includes/class-wp-customize-manager.php 598
add_action( 'after_setup_theme', array( $this, 'establish_loaded_changeset' ), 5 );
wp-includes/class-wp-customize-manager.php 606
add_action( 'after_setup_theme', array( $this, 'import_theme_starter_content' ), 100 );
wp-includes/default-filters.php 566
add_action( 'after_setup_theme', '_add_default_theme_supports', 1 );
wp-includes/default-filters.php 708
add_action( 'after_setup_theme', 'wp_setup_widgets_block_editor', 1 );
wp-includes/default-filters.php 781
add_action( 'after_setup_theme', 'wp_enable_block_templates', 1 );
wp-settings.php 460
add_action( 'after_setup_theme', array( wp_script_modules(), 'add_hooks' ) );
wp-settings.php 461
add_action( 'after_setup_theme', array( wp_interactivity(), 'add_hooks' ) );