当前浏览:首页 / WordPress钩子 / after_setup_theme

do_action( 'after_setup_theme' )

加载主题后触发

after

setup

theme 主题more...


说明

在每次页面加载期间,在主题初始化之后,都会调用该钩子。它通常用于执行主题的基本设置、注册和初始化操作。


源码

查看源码 官方文档


更新日志

版本描述
3.0.0开始引入

用户贡献的笔记

  • 贡献者:Aamer Shahzad

    当每个页面在主题初始化后加载时,就会调用这个钩子。这用于基本主题设置、主题功能和初始化钩子的注册。这个钩子的基本用法可以在WordPress安装附带的默认主题上看到。

    下面是 twentyfifteen 默认主题的示例代码。

    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: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
    	 */
    	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: https://codex.wordpress.org/Post_Formats
    	 */
    	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' );
  • 贡献者:Mahdi Yazdani

    主题可以注册自己的编辑器颜色,并有选择地锁定用户从定义的调色板中进行拾取。

    /**
     * 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' );
    
  • 贡献者:Ali Haider

    我就是这样尝试的,效果很好。

    <?php 
    	
    	add_action( 'after_setup_theme', 'wpdocs_i_am_a_function' );
    	
    	function wpdocs_i_am_a_function() {
    
    		add_theme_support( 'title-tag' );
    		add_theme_support( 'post-thumbnails' );
    		add_theme_support( 'custom-header' );
    
    	}
    
     ?>