shortcode_atts_(shortcode)хук-фильтрWP 3.6.0

Фильтрует дефолтные атрибуты указанного шорткода.

Фильтры вызывается в функции shortcode_atts(), только в том случае если в ней указан третий параметр $shortcode (название шорткода). Т.е. если функция вызывается для реального шорткода, например смотрите код шорткода gallery_shortcode().

Базовые варианты хука:

  • shortcode_atts_gallery
  • shortcode_atts_caption
  • shortcode_atts_playlist
  • shortcode_atts_audio
  • shortcode_atts_video
О хуке и функции shortcode_atts() в видеоформате:

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

add_filter( 'shortcode_atts_(shortcode)', 'wp_kama_shortcode_atts_filter', 10, 4 );

/**
 * Function for `shortcode_atts_(shortcode)` filter-hook.
 * 
 * @param array  $out       The output array of shortcode attributes.
 * @param array  $pairs     The supported attributes and their defaults.
 * @param array  $atts      The user defined shortcode attributes.
 * @param string $shortcode The shortcode name.
 *
 * @return array
 */
function wp_kama_shortcode_atts_filter( $out, $pairs, $atts, $shortcode ){

	// filter...
	return $out;
}
$out(массив)
Ассоциативный массив атрибутов шорткода, который будет изменяться при фильтрации.
$pairs(массив)
Массив всех возможных атрибутов шорткода и их значения по умолчанию.
$atts(массив)
Атрибуты указанные для шорткода пользователем.
$shortcode(строка)
Название шорткода, например gallery. Это название указывается в название этого фильтра, в конце.

Примеры

0

#1 Изменим количество колонок галереи по умолчанию

По умолчанию в галереи установлено 3 колонки, а нам нужно сделать 2. Т.е. если в тексте шорткод указан так [gallery_ ids="54,65,65"], то картинки будут выведены в 3 колонки, а нам нужно сделать в 2 колонки.

Для этого используем хук shortcode_atts_gallery и добавим получившийся код в файл темы functions.php

## Установим две колонки в галереи по умолчанию
add_filter( 'shortcode_atts_'.'gallery', 'set_default_gallery_columns', 10, 3);
function set_default_gallery_columns( $out, $pairs, $atts ){
	// изменим значение по умолчанию, если значение не установлено
	if( ! isset($atts['columns']) ){
		$out['columns'] = 2;
	}

	return $out;
}

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

С версии 3.6.0 Введена.
С версии 4.4.0 Added the $shortcode parameter.

Где вызывается хук

shortcode_atts()
shortcode_atts_(shortcode)
wp-includes/shortcodes.php 688
$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );

Где используется хук в WordPress

wp-includes/widgets/class-wp-widget-custom-html.php 131
add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
wp-includes/widgets/class-wp-widget-custom-html.php 168
remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
wp-includes/widgets/class-wp-widget-text.php 270
add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
wp-includes/widgets/class-wp-widget-text.php 323
remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );