shortcode_unautop() WP 2.9.0

Видаляє тег, що обертає <p>…</p> , якщо в ньому розташований тільки шорткод.

1 раз – 0.000067 сек
(дуже швидко) | 50000 разів – 0.67 сек
(дуже швидко)

Хуків немає.

Повертає

Строку. Змінений рядок.

Використання

shortcode_unautop($pee);
$pee
(рядок) (обов’язковий)
Текст, який потрібно відформатувати.

Приклади

0

#1 Демонстрація роботи

Допустимо у нас зареєстрований шорткод short. Тоді:

$str = '
<p>[short id="54"]</p>

текст

[short id="54"]

текст

<p>
[short id="54"]
</p>

текст

<p>[short id="54"] текст</p>

Шорткод із змістом

<p>[short align="aligncenter" width="495"]будь-який текст всередині[/short]</p>
';

echo shortcode_unautop($str);

/* Отримаємо:
[short id="54"]

текст

[short id="54"]

текст

[short id="54"]

текст

<p>[short id="54"] текст</p>

текст

[short align="aligncenter" width="495"]будь-який текст всередині[/short]
*/
0

#2 Яке регулярне вираження використовується

Приклад того, який регулярний вираз виходить в результаті роботи функції (залежить від шорткодів, які зареєстровані в даний момент) (версія wordpress 4.4):

(?:[rnt ]|xC2xA0| )*+([(embed|wp_caption|caption|gallery|playlist|audio|video|democracy|democracy_archives|download)(?![w -])[^]/]*(?:/(?!])[^]/]*)*?(?:/]|](?:[^[ ]*+(?:[(?!/])[^[]*+)*+[/])?))(?:[rnt ]|xC2 xA0| )*+</p>

нотатки

  • Global. Масив. $shortcode_tags

список змін

З версії 2.9.0Введено.

shortcode unautop WP 6.0.2

function shortcode_unautop( $text ) { Global $shortcode_tags; if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) { return $text; } $tagregexp = implode( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) ); $spaces = wp_spaces_regexp(); // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound,WordPress.WhiteSpace.PrecisionAlignment.Found -- don't remove regex indentation $ pattern = '/' . '<p>' // Opening paragraph. . '(?:' . $spaces . ')*+' // Optional leading whitespace. . '(' // 1: The shortcode. . '' // Opening bracket. . "($ tagregexp)" // 2: Shortcode name. . '(?![w-])' // Не сказано словом character or hyphen. // Unroll the loop: Inside the opening shortcode tag. . '[^]/]*' // Не closing bracket or forward slash. . '(?:' . '/(?!))' // A forward slash не followed by a closing bracket. . '[^]/]*' // Не closing bracket or forward slash. . ')*?' . '(?:' . '/]' // Self closing tag and closing bracket. . '|' . '' // Closing bracket. . '(?:' // Unroll the loop: Optionally, anything between the opening and closing shortcode tags. . '[^[]*+' // Not an opening bracket. . '(?:' . '[(?!/2])' // An opening bracket не followed by closing shortcode tag. . '[^[]*+' // Not an opening bracket. . ')*+' . '[/2'' // Closing shortcode tag. . ')?' . ')' . ')' . '(?:' . $spaces . ')*+' // Optional trailing whitespace. . '</' // Closing paragraph. . '/'; // phpcs:enable return preg_replace( $pattern, '$1', $text ); }