* Specific post formats can be registered by passing an array of types
* to add_theme_support().
* Specific areas of HTML5 support *must* be passed via an array to add_theme_support().
return in_array( $type, $_wp_theme_features[ $feature ][0], true );
case 'custom-background':
// Specific capabilities can be registered by passing an array to add_theme_support().
return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] );
* Filters whether the current theme supports a specific feature.
* The dynamic portion of the hook name, `$feature`, refers to the specific
* theme feature. See add_theme_support() for the list of possible values.
* @param bool $supports Whether the current theme supports the given feature. Default true.
* @param array $args Array of arguments for the feature.
* @param string $feature The theme feature.
return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
* Checks a theme's support for a given feature before loading the functions which implement it.
* @param string $feature The feature being checked. See add_theme_support() for the list
* @param string $include Path to the file.
* @return bool True if the current theme supports the supplied feature, false otherwise.
function require_if_theme_supports( $feature, $include ) {
if ( current_theme_supports( $feature ) ) {
* Registers a theme feature for use in add_theme_support().
* This does not indicate that the current theme supports the feature, it only describes
* the feature's supported options.
* @see add_theme_support()
* @global array $_wp_registered_theme_features
* @param string $feature The name uniquely identifying the feature. See add_theme_support()
* for the list of possible values.
* Data used to describe the theme.
* @type string $type The type of data associated with this feature.
* Valid values are 'string', 'boolean', 'integer',
* 'number', 'array', and 'object'. Defaults to 'boolean'.
* @type boolean $variadic Does this feature utilize the variadic support
* of add_theme_support(), or are all arguments specified
* as the second parameter. Must be used with the "array" type.
* @type string $description A short description of the feature. Included in
* the Themes REST API schema. Intended for developers.
* @type bool|array $show_in_rest {
* Whether this feature should be included in the Themes REST API endpoint.
* Defaults to not being included. When registering an 'array' or 'object' type,
* this argument must be an array with the 'schema' key.
* @type array $schema Specifies the JSON Schema definition describing
* the feature. If any objects in the schema do not include
* the 'additionalProperties' keyword, it is set to false.
* @type string $name An alternate name to be used as the property name
* @type callable $prepare_callback A function used to format the theme support in the REST API.
* Receives the raw theme support value.
* @return true|WP_Error True if the theme feature was successfully registered, a WP_Error object if not.
function register_theme_feature( $feature, $args = array() ) {
global $_wp_registered_theme_features;
if ( ! is_array( $_wp_registered_theme_features ) ) {
$_wp_registered_theme_features = array();
$args = wp_parse_args( $args, $defaults );
if ( true === $args['show_in_rest'] ) {
$args['show_in_rest'] = array();
if ( is_array( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = wp_parse_args(
'prepare_callback' => null,
if ( ! in_array( $args['type'], array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ), true ) ) {
__( 'The feature "type" is not valid JSON Schema type.' )
if ( true === $args['variadic'] && 'array' !== $args['type'] ) {
'variadic_must_be_array',
__( 'When registering a "variadic" theme feature, the "type" must be an "array".' )
if ( false !== $args['show_in_rest'] && in_array( $args['type'], array( 'array', 'object' ), true ) ) {
if ( ! is_array( $args['show_in_rest'] ) || empty( $args['show_in_rest']['schema'] ) ) {
__( 'When registering an "array" or "object" feature to show in the REST API, the feature\'s schema must also be defined.' )
if ( 'array' === $args['type'] && ! isset( $args['show_in_rest']['schema']['items'] ) ) {
__( 'When registering an "array" feature, the feature\'s schema must include the "items" keyword.' )
if ( 'object' === $args['type'] && ! isset( $args['show_in_rest']['schema']['properties'] ) ) {
'missing_schema_properties',
__( 'When registering an "object" feature, the feature\'s schema must include the "properties" keyword.' )
if ( is_array( $args['show_in_rest'] ) ) {
if ( isset( $args['show_in_rest']['prepare_callback'] ) && ! is_callable( $args['show_in_rest']['prepare_callback'] ) ) {
'invalid_rest_prepare_callback',
/* translators: %s: prepare_callback */
__( 'The "%s" must be a callable function.' ),
$args['show_in_rest']['schema'] = wp_parse_args(
$args['show_in_rest']['schema'],
'description' => $args['description'],
if ( is_bool( $args['show_in_rest']['schema']['default'] )
&& ! in_array( 'boolean', (array) $args['show_in_rest']['schema']['type'], true )
// Automatically include the "boolean" type when the default value is a boolean.
$args['show_in_rest']['schema']['type'] = (array) $args['show_in_rest']['schema']['type'];
array_unshift( $args['show_in_rest']['schema']['type'], 'boolean' );
$args['show_in_rest']['schema'] = rest_default_additional_properties_to_false( $args['show_in_rest']['schema'] );
$_wp_registered_theme_features[ $feature ] = $args;
* Gets the list of registered theme features.
* @global array $_wp_registered_theme_features
* @return array[] List of theme features, keyed by their name.
function get_registered_theme_features() {
global $_wp_registered_theme_features;
if ( ! is_array( $_wp_registered_theme_features ) ) {
return $_wp_registered_theme_features;
* Gets the registration config for a theme feature.
* @global array $_wp_registered_theme_features
* @param string $feature The feature name. See add_theme_support() for the list
* @return array|null The registration args, or null if the feature was not registered.
function get_registered_theme_feature( $feature ) {
global $_wp_registered_theme_features;
if ( ! is_array( $_wp_registered_theme_features ) ) {
return isset( $_wp_registered_theme_features[ $feature ] ) ? $_wp_registered_theme_features[ $feature ] : null;
* Checks an attachment being deleted to see if it's a header or background image.
* If true it removes the theme modification which would be pointing at the deleted
* @since 4.3.0 Also removes `header_image_data`.
* @since 4.5.0 Also removes custom logo theme mods.
* @param int $id The attachment ID.
function _delete_attachment_theme_mod( $id ) {
$attachment_image = wp_get_attachment_url( $id );
$header_image = get_header_image();
$background_image = get_background_image();
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( $custom_logo_id && $custom_logo_id == $id ) {
remove_theme_mod( 'custom_logo' );
remove_theme_mod( 'header_text' );
if ( $header_image && $header_image == $attachment_image ) {
remove_theme_mod( 'header_image' );
remove_theme_mod( 'header_image_data' );
if ( $background_image && $background_image == $attachment_image ) {
remove_theme_mod( 'background_image' );
* Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
* See {@see 'after_switch_theme'}.
function check_theme_switched() {
$stylesheet = get_option( 'theme_switched' );
$old_theme = wp_get_theme( $stylesheet );
// Prevent widget & menu mapping from running since Customizer already called it up front.
if ( get_option( 'theme_switched_via_customizer' ) ) {
remove_action( 'after_switch_theme', '_wp_menus_changed' );
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
update_option( 'theme_switched_via_customizer', false );
if ( $old_theme->exists() ) {
* Fires on the first WP load after a theme switch if the old theme still exists.
* This action fires multiple times and the parameters differs
* according to the context, if the old theme exists or not.
* If the old theme is missing, the parameter will be the slug
* @param string $old_name Old theme name.
* @param WP_Theme $old_theme WP_Theme instance of the old theme.
do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
/** This action is documented in wp-includes/theme.php */
do_action( 'after_switch_theme', $stylesheet, $old_theme );
update_option( 'theme_switched', false );
* Includes and instantiates the WP_Customize_Manager class.
* Loads the Customizer at plugins_loaded when accessing the customize.php admin
* page or when any request includes a wp_customize=on param or a customize_changeset
* param (a UUID). This param is a signal for whether to bootstrap the Customizer when
* WordPress is loading, especially in the Customizer preview
* or when making Customizer Ajax requests for widgets or menus.
* @global WP_Customize_Manager $wp_customize
function _wp_customize_include() {
$is_customize_admin_page = ( is_admin() && 'customize.php' === basename( $_SERVER['PHP_SELF'] ) );
( isset( $_REQUEST['wp_customize'] ) && 'on' === $_REQUEST['wp_customize'] )
( ! empty( $_GET['customize_changeset_uuid'] ) || ! empty( $_POST['customize_changeset_uuid'] ) )
if ( ! $should_include ) {
* Note that wp_unslash() is not being used on the input vars because it is
* called before wp_magic_quotes() gets called. Besides this fact, none of
* the values should contain any characters needing slashes anyway.
$keys = array( 'changeset_uuid', 'customize_changeset_uuid', 'customize_theme', 'theme', 'customize_messenger_channel', 'customize_autosaved' );
$input_vars = array_merge(
wp_array_slice_assoc( $_GET, $keys ),
wp_array_slice_assoc( $_POST, $keys )
$messenger_channel = null;
// Value false indicates UUID should be determined after_setup_theme
// to either re-use existing saved changeset or else generate a new UUID if none exists.
// Set initially fo false since defaults to true for back-compat;
// can be overridden via the customize_changeset_branching filter.
if ( $is_customize_admin_page && isset( $input_vars['changeset_uuid'] ) ) {
$changeset_uuid = sanitize_key( $input_vars['changeset_uuid'] );
} elseif ( ! empty( $input_vars['customize_changeset_uuid'] ) ) {
$changeset_uuid = sanitize_key( $input_vars['customize_changeset_uuid'] );
// Note that theme will be sanitized via WP_Theme.
if ( $is_customize_admin_page && isset( $input_vars['theme'] ) ) {
$theme = $input_vars['theme'];
} elseif ( isset( $input_vars['customize_theme'] ) ) {
$theme = $input_vars['customize_theme'];
if ( ! empty( $input_vars['customize_autosaved'] ) ) {
if ( isset( $input_vars['customize_messenger_channel'] ) ) {
$messenger_channel = sanitize_key( $input_vars['customize_messenger_channel'] );
* Note that settings must be previewed even outside the customizer preview
* and also in the customizer pane itself. This is to enable loading an existing
* changeset into the customizer. Previewing the settings only has to be prevented
* here in the case of a customize_save action because this will cause WP to think
* there is nothing changed that needs to be saved.
$is_customize_save_action = (
isset( $_REQUEST['action'] )
'customize_save' === wp_unslash( $_REQUEST['action'] )
$settings_previewed = ! $is_customize_save_action;
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$GLOBALS['wp_customize'] = new WP_Customize_Manager( compact( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed', 'autosaved', 'branching' ) );
* Publishes a snapshot's changes.
* @global wpdb $wpdb WordPress database abstraction object.
* @global WP_Customize_Manager $wp_customize Customizer instance.
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $changeset_post Changeset post object.
function _wp_customize_publish_changeset( $new_status, $old_status, $changeset_post ) {
global $wp_customize, $wpdb;
$is_publishing_changeset = (
'customize_changeset' === $changeset_post->post_type
'publish' === $new_status
'publish' !== $old_status
if ( ! $is_publishing_changeset ) {
if ( empty( $wp_customize ) ) {
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager(
'changeset_uuid' => $changeset_post->post_name,
'settings_previewed' => false,
if ( ! did_action( 'customize_register' ) ) {
* When running from CLI or Cron, the customize_register action will need
* to be triggered in order for core, themes, and plugins to register their
* settings. Normally core will add_action( 'customize_register' ) at
* priority 10 to register the core settings, and if any themes/plugins
* also add_action( 'customize_register' ) at the same priority, they
* will have a $wp_customize with those settings registered since they
* call add_action() afterward, normally. However, when manually doing
* the customize_register action after the setup_theme, then the order
* will be reversed for two actions added at priority 10, resulting in
* the core settings no longer being available as expected to themes/plugins.
* So the following manually calls the method that registers the core
* settings up front before doing the action.
remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) );
$wp_customize->register_controls();
/** This filter is documented in /wp-includes/class-wp-customize-manager.php */
do_action( 'customize_register', $wp_customize );
$wp_customize->_publish_changeset_values( $changeset_post->ID );
* Trash the changeset post if revisions are not enabled. Unpublished
* changesets by default get garbage collected due to the auto-draft status.
* When a changeset post is published, however, it would no longer get cleaned
* out. This is a problem when the changeset posts are never displayed anywhere,
* since they would just be endlessly piling up. So here we use the revisions
* feature to indicate whether or not a published changeset should get trashed
* and thus garbage collected.
if ( ! wp_revisions_enabled( $changeset_post ) ) {
$wp_customize->trash_changeset_post( $changeset_post->ID );
* Filters changeset post data upon insert to ensure post_name is intact.
* This is needed to prevent the post_name from being dropped when the post is
* transitioned into pending status by a contributor.
* @param array $post_data An array of slashed post data.
* @param array $supplied_post_data An array of sanitized, but otherwise unmodified post data.
* @return array Filtered data.
function _wp_customize_changeset_filter_insert_post_data( $post_data, $supplied_post_data ) {
if ( isset( $post_data['post_type'] ) && 'customize_changeset' === $post_data['post_type'] ) {
// Prevent post_name from being dropped, such as when contributor saves a changeset post as pending.
if ( empty( $post_data['post_name'] ) && ! empty( $supplied_post_data['post_name'] ) ) {
$post_data['post_name'] = $supplied_post_data['post_name'];