钩子文档

after_setup_theme

💡 云策文档标注

概述

after_setup_theme 是一个 WordPress 钩子,在主题加载后触发,用于执行主题的基本设置、功能注册和初始化操作。它在每个页面加载时调用,适用于主题开发中的早期配置。

关键要点

  • after_setup_theme 钩子在主题初始化后、init 钩子之前运行,适合设置主题默认值和注册功能。
  • 常用于添加主题支持(如 post-thumbnails、title-tag、html5)、注册导航菜单、加载翻译文件等。
  • 示例代码展示了如何通过 add_action 将自定义函数(如 twentyfifteen_setup)挂载到此钩子。
  • 用户贡献笔记提供了额外用例,如注册编辑器颜色调色板,增强主题定制性。

代码示例

if ( ! function_exists( 'twentyfifteen_setup' ) ) :
function twentyfifteen_setup() {
    load_theme_textdomain( 'twentyfifteen', get_template_directory() . '/languages' );
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'title-tag' );
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 825, 510, true );
    register_nav_menus( array(
        'primary' => __( 'Primary Menu', 'twentyfifteen' ),
        'social'  => __( 'Social Links Menu', 'twentyfifteen' ),
    ) );
    add_theme_support( 'html5', array(
        'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
    ) );
    add_theme_support( 'post-formats', array(
        'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
    ) );
    add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
        'default-color'      => $default_color,
        'default-attachment' => 'fixed',
    ) ) );
    add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
}
endif;
add_action( 'after_setup_theme', 'twentyfifteen_setup' );

📄 原文内容

Fires after the theme is loaded.

More Information

This hook is called during each page load, after the theme is initialized. It is generally used to perform basic setup, registration, and init actions for a theme.

Source

do_action( 'after_setup_theme' );

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    This Hook is called when each page is loaded after theme is initialised. This is used for the basic theme setup, registration of the theme features and init hooks. The basic use of this hook can be seen on the default themes that comes with WordPress Installation.

    Following is the example code from twentyfifteen default theme.

    if ( ! function_exists( 'twentyfifteen_setup' ) ) :
    /**
     * Sets up theme defaults and registers support for various WordPress features.
     *
     * Note that this function is hooked into the after_setup_theme hook, which
     * runs before the init hook. The init hook is too late for some features, such
     * as indicating support for post thumbnails.
     *
     * @since Twenty Fifteen 1.0
     */
    function twentyfifteen_setup() {
    
    	/*
    	 * Make theme available for translation.
    	 * Translations can be filed in the /languages/ directory.
    	 * If you're building a theme based on twentyfifteen, use a find and replace
    	 * to change 'twentyfifteen' to the name of your theme in all the template files
    	 */
    	load_theme_textdomain( 'twentyfifteen', get_template_directory() . '/languages' );
    
    	// Add default posts and comments RSS feed links to head.
    	add_theme_support( 'automatic-feed-links' );
    
    	/*
    	 * Let WordPress manage the document title.
    	 * By adding theme support, we declare that this theme does not use a
    	 * hard-coded  tag in the document head, and expect WordPress to
    	 * provide it for us.
    	 */
    	add_theme_support( 'title-tag' );
    
    	/*
    	 * Enable support for Post Thumbnails on posts and pages.
    	 *
    	 * See: <a href="https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails" rel="nofollow ugc">https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails</a>
    	 */
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 825, 510, true );
    
    	// This theme uses wp_nav_menu() in two locations.
    	register_nav_menus( array(
    		'primary' => __( 'Primary Menu',      'twentyfifteen' ),
    		'social'  => __( 'Social Links Menu', 'twentyfifteen' ),
    	) );
    
    	/*
    	 * Switch default core markup for search form, comment form, and comments
    	 * to output valid HTML5.
    	 */
    	add_theme_support( 'html5', array(
    		'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
    	) );
    
    	/*
    	 * Enable support for Post Formats.
    	 *
    	 * See: <a href="https://codex.wordpress.org/Post_Formats" rel="nofollow ugc">https://codex.wordpress.org/Post_Formats</a>
    	 */
    	add_theme_support( 'post-formats', array(
    		'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
    	) );
    
    	$color_scheme  = twentyfifteen_get_color_scheme();
    	$default_color = trim( $color_scheme[0], '#' );
    
    	// Setup the WordPress core custom background feature.
    	add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
    		'default-color'      => $default_color,
    		'default-attachment' => 'fixed',
    	) ) );
    
    	/*
    	 * This theme styles the visual editor to resemble the theme style,
    	 * specifically font, colors, icons, and column width.
    	 */
    	add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
    }
    endif; // twentyfifteen_setup
    add_action( 'after_setup_theme', 'twentyfifteen_setup' );

  2. Skip to note 6 content

    Feel free to copy and paste this code into your functions.php file for a basic setup(common line for every wp theme ) –

    </pre>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-6847' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fafter_setup_theme%2F%3Freplytocom%3D6847%23feedback-editor-6847" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-3781" data-comment-id="3781" class="comment byuser comment-author-mahdiyazdani even thread-even depth-1">
    			<article id="div-comment-3781" class="comment-body">
    
    							<a href="#comment-content-3781" class="screen-reader-text">Skip to note 7 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/mahdiyazdani/" rel="external nofollow" class="url">Mahdi Yazdani</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/hooks/after_setup_theme/#comment-3781">
    							<time datetime="2020-04-18T11:20:42+00:00">
    							6 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="22c60f321e" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="3781" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fafter_setup_theme%2F%23comment-3781"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title=""><span class="screen-reader-text">Vote results for this note: </span>0</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="3781" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fafter_setup_theme%2F%23comment-3781"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-3781">
    				<p>Themes can register their own editor colors and optionally lock users into picking from the defined palette.</p>
    <pre class="wp-block-code"><code lang="php" class="language-php line-numbers">/**
     * Overwrite block editor’s default color palette. 
     *
     * @return void
     */
    function prefix_editor_color_palette() {
    	add_theme_support( 'editor-color-palette', array(
    		array(
    		    'name'  => __( 'Storm Gray', 'themeLangDomain' ),
    		    'slug'  => 'storm-gray',
    		    'color' => '#6B6F82',
    		),
    		array(
    		    'name'  => __( 'Martinique', 'themeLangDomain' ),
    		    'slug'  => 'martinique',
    		    'color' => '#2D2E4F',
    		),
    		array(
    		    'name'  => __( 'Cornflower Blue', 'themeLangDomain' ),
    		    'slug'  => 'cornflower-blue',
    		    'color' => '#666EE8',
    		),
    		array(
    		    'name'  => __( 'Radical Red', 'themeLangDomain' ),
    		    'slug'  => 'radical-red',
    		    'color' => '#FF4961',
    		),
        ) );
    }
    add_action( 'after_setup_theme', 'prefix_editor_color_palette' );