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 output array shortcode attributes.
* @param array $pairs Допоможуть атрибути і їх defaults.
* @param array $atts 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. Ця назва вказується в назву цього фільтра в кінці.
Приклади
#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 $shortcode parameter. |
Де викликається хук
shortcode_atts_(shortcode)
wp-includes/shortcodes.php 610
$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );Де використовується хук у WordPress
wp-includes/widgets/class-wp-widget-text.php 268
add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );