get_theme_support()WP 3.1.0

Получает аргументы указанной возможности (фитчи), которые были переданы при регистрации этой возможности для темы (шаблона).

Возможность темы регистрируется функцией add_theme_support() и иногда при регистрации передаются параметры. get_theme_support() получает переданные аргументы.

Все возможности хранятся в глобальной переменной $_wp_theme_features.

1 раз — 0.000015 сек (очень быстро) | 50000 раз — 0.02 сек (скорость света)

Хуков нет.

Возвращает

Разное. Массив/строку/число/объект. Массив аргументов или значение зарегистрированной возможности.

Использование

get_theme_support( $feature );
$feature(строка) (обязательный)
Возможность (фитча) аргументы которой нужно получить.

Примеры

0

#1 Получим аргументы возможности темы 'html5'

$args = get_theme_support( 'html5' );

print_r( $args );
/* выведет:
Array
(
	[0] => Array
		(
			[0] => comment-list
			[1] => comment-form
			[2] => search-form
			[3] => gallery
			[4] => caption
		)

)
*/
0

#2 Как выглядит переменная $_wp_theme_features:

global $_wp_theme_features;

print_r( $_wp_theme_features );
/* выведет:
Array
(
	[menus] => 1
	[post-thumbnails] => 1
	[html5] => Array
		(
			[0] => Array
				(
					[0] => comment-list
					[1] => comment-form
					[2] => search-form
					[3] => gallery
					[4] => caption
				)

		)

	[widgets] => 1
)
*/
0

#3 Возможность post-thumbnails

$supports = get_theme_support('post-thumbnails');

// Теперь supports может быть двух вариантов. 
// Зависит от того как регистрировалась возможность

// если регалась без параметров:
// add_theme_support( 'post-thumbnails' );
// $supports будет равно true

// если с параметрами: 
// add_theme_support( 'post-thumbnails', array('post', 'page') ); 
// $supports будет равно такому массиву
/*
Array
(
	[0] => Array
		(
			[0] => post
			[1] => page
		)

)
*/

Заметки

  • Global. Массив. $_wp_theme_features

Список изменений

С версии 3.1.0 Введена.
С версии 5.3.0 Formalized the existing and already documented ...$args parameter by adding it to the function signature.

Код get_theme_support() WP 6.5.2

function get_theme_support( $feature, ...$args ) {
	global $_wp_theme_features;

	if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
		return false;
	}

	if ( ! $args ) {
		return $_wp_theme_features[ $feature ];
	}

	switch ( $feature ) {
		case 'custom-logo':
		case 'custom-header':
		case 'custom-background':
			if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) ) {
				return $_wp_theme_features[ $feature ][0][ $args[0] ];
			}
			return false;

		default:
			return $_wp_theme_features[ $feature ];
	}
}