shortcodes.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. /**
  3. * WordPress API for creating bbcode-like tags or what WordPress calls
  4. * "shortcodes". The tag and attribute parsing or regular expression code is
  5. * based on the Textpattern tag parser.
  6. *
  7. * A few examples are below:
  8. *
  9. * [shortcode /]
  10. * [shortcode foo="bar" baz="bing" /]
  11. * [shortcode foo="bar"]content[/shortcode]
  12. *
  13. * Shortcode tags support attributes and enclosed content, but does not entirely
  14. * support inline shortcodes in other shortcodes. You will have to call the
  15. * shortcode parser in your function to account for that.
  16. *
  17. * {@internal
  18. * Please be aware that the above note was made during the beta of WordPress 2.6
  19. * and in the future may not be accurate. Please update the note when it is no
  20. * longer the case.}}
  21. *
  22. * To apply shortcode tags to content:
  23. *
  24. * $out = do_shortcode( $content );
  25. *
  26. * @link https://developer.wordpress.org/plugins/shortcodes/
  27. *
  28. * @package WordPress
  29. * @subpackage Shortcodes
  30. * @since 2.5.0
  31. */
  32. /**
  33. * Container for storing shortcode tags and their hook to call for the shortcode.
  34. *
  35. * @since 2.5.0
  36. *
  37. * @name $shortcode_tags
  38. * @var array
  39. * @global array $shortcode_tags
  40. */
  41. $shortcode_tags = array();
  42. /**
  43. * Adds a new shortcode.
  44. *
  45. * Care should be taken through prefixing or other means to ensure that the
  46. * shortcode tag being added is unique and will not conflict with other,
  47. * already-added shortcode tags. In the event of a duplicated tag, the tag
  48. * loaded last will take precedence.
  49. *
  50. * @since 2.5.0
  51. *
  52. * @global array $shortcode_tags
  53. *
  54. * @param string $tag Shortcode tag to be searched in post content.
  55. * @param callable $callback The callback function to run when the shortcode is found.
  56. * Every shortcode callback is passed three parameters by default,
  57. * including an array of attributes (`$atts`), the shortcode content
  58. * or null if not set (`$content`), and finally the shortcode tag
  59. * itself (`$shortcode_tag`), in that order.
  60. */
  61. function add_shortcode( $tag, $callback ) {
  62. global $shortcode_tags;
  63. if ( '' === trim( $tag ) ) {
  64. _doing_it_wrong(
  65. __FUNCTION__,
  66. __( 'Invalid shortcode name: Empty name given.' ),
  67. '4.4.0'
  68. );
  69. return;
  70. }
  71. if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
  72. _doing_it_wrong(
  73. __FUNCTION__,
  74. sprintf(
  75. /* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */
  76. __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ),
  77. $tag,
  78. '& / < > [ ] ='
  79. ),
  80. '4.4.0'
  81. );
  82. return;
  83. }
  84. $shortcode_tags[ $tag ] = $callback;
  85. }
  86. /**
  87. * Removes hook for shortcode.
  88. *
  89. * @since 2.5.0
  90. *
  91. * @global array $shortcode_tags
  92. *
  93. * @param string $tag Shortcode tag to remove hook for.
  94. */
  95. function remove_shortcode( $tag ) {
  96. global $shortcode_tags;
  97. unset( $shortcode_tags[ $tag ] );
  98. }
  99. /**
  100. * Clears all shortcodes.
  101. *
  102. * This function clears all of the shortcode tags by replacing the shortcodes global with
  103. * an empty array. This is actually an efficient method for removing all shortcodes.
  104. *
  105. * @since 2.5.0
  106. *
  107. * @global array $shortcode_tags
  108. */
  109. function remove_all_shortcodes() {
  110. global $shortcode_tags;
  111. $shortcode_tags = array();
  112. }
  113. /**
  114. * Determines whether a registered shortcode exists named $tag.
  115. *
  116. * @since 3.6.0
  117. *
  118. * @global array $shortcode_tags List of shortcode tags and their callback hooks.
  119. *
  120. * @param string $tag Shortcode tag to check.
  121. * @return bool Whether the given shortcode exists.
  122. */
  123. function shortcode_exists( $tag ) {
  124. global $shortcode_tags;
  125. return array_key_exists( $tag, $shortcode_tags );
  126. }
  127. /**
  128. * Determines whether the passed content contains the specified shortcode.
  129. *
  130. * @since 3.6.0
  131. *
  132. * @global array $shortcode_tags
  133. *
  134. * @param string $content Content to search for shortcodes.
  135. * @param string $tag Shortcode tag to check.
  136. * @return bool Whether the passed content contains the given shortcode.
  137. */
  138. function has_shortcode( $content, $tag ) {
  139. if ( false === strpos( $content, '[' ) ) {
  140. return false;
  141. }
  142. if ( shortcode_exists( $tag ) ) {
  143. preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
  144. if ( empty( $matches ) ) {
  145. return false;
  146. }
  147. foreach ( $matches as $shortcode ) {
  148. if ( $tag === $shortcode[2] ) {
  149. return true;
  150. } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
  151. return true;
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * Searches content for shortcodes and filter shortcodes through their hooks.
  159. *
  160. * This function is an alias for do_shortcode().
  161. *
  162. * @since 5.4.0
  163. *
  164. * @see do_shortcode()
  165. *
  166. * @param string $content Content to search for shortcodes.
  167. * @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped.
  168. * Default false.
  169. * @return string Content with shortcodes filtered out.
  170. */
  171. function apply_shortcodes( $content, $ignore_html = false ) {
  172. return do_shortcode( $content, $ignore_html );
  173. }
  174. /**
  175. * Searches content for shortcodes and filter shortcodes through their hooks.
  176. *
  177. * If there are no shortcode tags defined, then the content will be returned
  178. * without any filtering. This might cause issues when plugins are disabled but
  179. * the shortcode will still show up in the post or content.
  180. *
  181. * @since 2.5.0
  182. *
  183. * @global array $shortcode_tags List of shortcode tags and their callback hooks.
  184. *
  185. * @param string $content Content to search for shortcodes.
  186. * @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped.
  187. * Default false.
  188. * @return string Content with shortcodes filtered out.
  189. */
  190. function do_shortcode( $content, $ignore_html = false ) {
  191. global $shortcode_tags;
  192. if ( false === strpos( $content, '[' ) ) {
  193. return $content;
  194. }
  195. if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
  196. return $content;
  197. }
  198. // Find all registered tag names in $content.
  199. preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
  200. $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
  201. if ( empty( $tagnames ) ) {
  202. return $content;
  203. }
  204. $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames );
  205. $pattern = get_shortcode_regex( $tagnames );
  206. $content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content );
  207. // Always restore square braces so we don't break things like <!--[if IE ]>.
  208. $content = unescape_invalid_shortcodes( $content );
  209. return $content;
  210. }
  211. /**
  212. * Retrieves the shortcode regular expression for searching.
  213. *
  214. * The regular expression combines the shortcode tags in the regular expression
  215. * in a regex class.
  216. *
  217. * The regular expression contains 6 different sub matches to help with parsing.
  218. *
  219. * 1 - An extra [ to allow for escaping shortcodes with double [[]]
  220. * 2 - The shortcode name
  221. * 3 - The shortcode argument list
  222. * 4 - The self closing /
  223. * 5 - The content of a shortcode when it wraps some content.
  224. * 6 - An extra ] to allow for escaping shortcodes with double [[]]
  225. *
  226. * @since 2.5.0
  227. * @since 4.4.0 Added the `$tagnames` parameter.
  228. *
  229. * @global array $shortcode_tags
  230. *
  231. * @param array $tagnames Optional. List of shortcodes to find. Defaults to all registered shortcodes.
  232. * @return string The shortcode search regular expression
  233. */
  234. function get_shortcode_regex( $tagnames = null ) {
  235. global $shortcode_tags;
  236. if ( empty( $tagnames ) ) {
  237. $tagnames = array_keys( $shortcode_tags );
  238. }
  239. $tagregexp = implode( '|', array_map( 'preg_quote', $tagnames ) );
  240. // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag().
  241. // Also, see shortcode_unautop() and shortcode.js.
  242. // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
  243. return '\\[' // Opening bracket.
  244. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]].
  245. . "($tagregexp)" // 2: Shortcode name.
  246. . '(?![\\w-])' // Not followed by word character or hyphen.
  247. . '(' // 3: Unroll the loop: Inside the opening shortcode tag.
  248. . '[^\\]\\/]*' // Not a closing bracket or forward slash.
  249. . '(?:'
  250. . '\\/(?!\\])' // A forward slash not followed by a closing bracket.
  251. . '[^\\]\\/]*' // Not a closing bracket or forward slash.
  252. . ')*?'
  253. . ')'
  254. . '(?:'
  255. . '(\\/)' // 4: Self closing tag...
  256. . '\\]' // ...and closing bracket.
  257. . '|'
  258. . '\\]' // Closing bracket.
  259. . '(?:'
  260. . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags.
  261. . '[^\\[]*+' // Not an opening bracket.
  262. . '(?:'
  263. . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag.
  264. . '[^\\[]*+' // Not an opening bracket.
  265. . ')*+'
  266. . ')'
  267. . '\\[\\/\\2\\]' // Closing shortcode tag.
  268. . ')?'
  269. . ')'
  270. . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]].
  271. // phpcs:enable
  272. }
  273. /**
  274. * Regular Expression callable for do_shortcode() for calling shortcode hook.
  275. *
  276. * @see get_shortcode_regex() for details of the match array contents.
  277. *
  278. * @since 2.5.0
  279. * @access private
  280. *
  281. * @global array $shortcode_tags
  282. *
  283. * @param array $m Regular expression match array.
  284. * @return string|false Shortcode output on success, false on failure.
  285. */
  286. function do_shortcode_tag( $m ) {
  287. global $shortcode_tags;
  288. // Allow [[foo]] syntax for escaping a tag.
  289. if ( '[' === $m[1] && ']' === $m[6] ) {
  290. return substr( $m[0], 1, -1 );
  291. }
  292. $tag = $m[2];
  293. $attr = shortcode_parse_atts( $m[3] );
  294. if ( ! is_callable( $shortcode_tags[ $tag ] ) ) {
  295. _doing_it_wrong(
  296. __FUNCTION__,
  297. /* translators: %s: Shortcode tag. */
  298. sprintf( __( 'Attempting to parse a shortcode without a valid callback: %s' ), $tag ),
  299. '4.3.0'
  300. );
  301. return $m[0];
  302. }
  303. /**
  304. * Filters whether to call a shortcode callback.
  305. *
  306. * Returning a non-false value from filter will short-circuit the
  307. * shortcode generation process, returning that value instead.
  308. *
  309. * @since 4.7.0
  310. *
  311. * @param false|string $return Short-circuit return value. Either false or the value to replace the shortcode with.
  312. * @param string $tag Shortcode name.
  313. * @param array|string $attr Shortcode attributes array or empty string.
  314. * @param array $m Regular expression match array.
  315. */
  316. $return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m );
  317. if ( false !== $return ) {
  318. return $return;
  319. }
  320. $content = isset( $m[5] ) ? $m[5] : null;
  321. $output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6];
  322. /**
  323. * Filters the output created by a shortcode callback.
  324. *
  325. * @since 4.7.0
  326. *
  327. * @param string $output Shortcode output.
  328. * @param string $tag Shortcode name.
  329. * @param array|string $attr Shortcode attributes array or empty string.
  330. * @param array $m Regular expression match array.
  331. */
  332. return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m );
  333. }
  334. /**
  335. * Searches only inside HTML elements for shortcodes and process them.
  336. *
  337. * Any [ or ] characters remaining inside elements will be HTML encoded
  338. * to prevent interference with shortcodes that are outside the elements.
  339. * Assumes $content processed by KSES already. Users with unfiltered_html
  340. * capability may get unexpected output if angle braces are nested in tags.
  341. *
  342. * @since 4.2.3
  343. *
  344. * @param string $content Content to search for shortcodes.
  345. * @param bool $ignore_html When true, all square braces inside elements will be encoded.
  346. * @param array $tagnames List of shortcodes to find.
  347. * @return string Content with shortcodes filtered out.
  348. */
  349. function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
  350. // Normalize entities in unfiltered HTML before adding placeholders.
  351. $trans = array(
  352. '&#91;' => '&#091;',
  353. '&#93;' => '&#093;',
  354. );
  355. $content = strtr( $content, $trans );
  356. $trans = array(
  357. '[' => '&#91;',
  358. ']' => '&#93;',
  359. );
  360. $pattern = get_shortcode_regex( $tagnames );
  361. $textarr = wp_html_split( $content );
  362. foreach ( $textarr as &$element ) {
  363. if ( '' === $element || '<' !== $element[0] ) {
  364. continue;
  365. }
  366. $noopen = false === strpos( $element, '[' );
  367. $noclose = false === strpos( $element, ']' );
  368. if ( $noopen || $noclose ) {
  369. // This element does not contain shortcodes.
  370. if ( $noopen xor $noclose ) {
  371. // Need to encode stray '[' or ']' chars.
  372. $element = strtr( $element, $trans );
  373. }
  374. continue;
  375. }
  376. if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
  377. // Encode all '[' and ']' chars.
  378. $element = strtr( $element, $trans );
  379. continue;
  380. }
  381. $attributes = wp_kses_attr_parse( $element );
  382. if ( false === $attributes ) {
  383. // Some plugins are doing things like [name] <[email]>.
  384. if ( 1 === preg_match( '%^<\s*\[\[?[^\[\]]+\]%', $element ) ) {
  385. $element = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $element );
  386. }
  387. // Looks like we found some crazy unfiltered HTML. Skipping it for sanity.
  388. $element = strtr( $element, $trans );
  389. continue;
  390. }
  391. // Get element name.
  392. $front = array_shift( $attributes );
  393. $back = array_pop( $attributes );
  394. $matches = array();
  395. preg_match( '%[a-zA-Z0-9]+%', $front, $matches );
  396. $elname = $matches[0];
  397. // Look for shortcodes in each attribute separately.
  398. foreach ( $attributes as &$attr ) {
  399. $open = strpos( $attr, '[' );
  400. $close = strpos( $attr, ']' );
  401. if ( false === $open || false === $close ) {
  402. continue; // Go to next attribute. Square braces will be escaped at end of loop.
  403. }
  404. $double = strpos( $attr, '"' );
  405. $single = strpos( $attr, "'" );
  406. if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) {
  407. /*
  408. * $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.
  409. * In this specific situation we assume KSES did not run because the input
  410. * was written by an administrator, so we should avoid changing the output
  411. * and we do not need to run KSES here.
  412. */
  413. $attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr );
  414. } else {
  415. // $attr like 'name = "[shortcode]"' or "name = '[shortcode]'".
  416. // We do not know if $content was unfiltered. Assume KSES ran before shortcodes.
  417. $count = 0;
  418. $new_attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr, -1, $count );
  419. if ( $count > 0 ) {
  420. // Sanitize the shortcode output using KSES.
  421. $new_attr = wp_kses_one_attr( $new_attr, $elname );
  422. if ( '' !== trim( $new_attr ) ) {
  423. // The shortcode is safe to use now.
  424. $attr = $new_attr;
  425. }
  426. }
  427. }
  428. }
  429. $element = $front . implode( '', $attributes ) . $back;
  430. // Now encode any remaining '[' or ']' chars.
  431. $element = strtr( $element, $trans );
  432. }
  433. $content = implode( '', $textarr );
  434. return $content;
  435. }
  436. /**
  437. * Removes placeholders added by do_shortcodes_in_html_tags().
  438. *
  439. * @since 4.2.3
  440. *
  441. * @param string $content Content to search for placeholders.
  442. * @return string Content with placeholders removed.
  443. */
  444. function unescape_invalid_shortcodes( $content ) {
  445. // Clean up entire string, avoids re-parsing HTML.
  446. $trans = array(
  447. '&#91;' => '[',
  448. '&#93;' => ']',
  449. );
  450. $content = strtr( $content, $trans );
  451. return $content;
  452. }
  453. /**
  454. * Retrieves the shortcode attributes regex.
  455. *
  456. * @since 4.4.0
  457. *
  458. * @return string The shortcode attribute regular expression.
  459. */
  460. function get_shortcode_atts_regex() {
  461. return '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|\'([^\']*)\'(?:\s|$)|(\S+)(?:\s|$)/';
  462. }
  463. /**
  464. * Retrieves all attributes from the shortcodes tag.
  465. *
  466. * The attributes list has the attribute name as the key and the value of the
  467. * attribute as the value in the key/value pair. This allows for easier
  468. * retrieval of the attributes, since all attributes have to be known.
  469. *
  470. * @since 2.5.0
  471. *
  472. * @param string $text
  473. * @return array|string List of attribute values.
  474. * Returns empty array if '""' === trim( $text ).
  475. * Returns empty string if '' === trim( $text ).
  476. * All other matches are checked for not empty().
  477. */
  478. function shortcode_parse_atts( $text ) {
  479. $atts = array();
  480. $pattern = get_shortcode_atts_regex();
  481. $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text );
  482. if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) {
  483. foreach ( $match as $m ) {
  484. if ( ! empty( $m[1] ) ) {
  485. $atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] );
  486. } elseif ( ! empty( $m[3] ) ) {
  487. $atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] );
  488. } elseif ( ! empty( $m[5] ) ) {
  489. $atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] );
  490. } elseif ( isset( $m[7] ) && strlen( $m[7] ) ) {
  491. $atts[] = stripcslashes( $m[7] );
  492. } elseif ( isset( $m[8] ) && strlen( $m[8] ) ) {
  493. $atts[] = stripcslashes( $m[8] );
  494. } elseif ( isset( $m[9] ) ) {
  495. $atts[] = stripcslashes( $m[9] );
  496. }
  497. }
  498. // Reject any unclosed HTML elements.
  499. foreach ( $atts as &$value ) {
  500. if ( false !== strpos( $value, '<' ) ) {
  501. if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) {
  502. $value = '';
  503. }
  504. }
  505. }
  506. } else {
  507. $atts = ltrim( $text );
  508. }
  509. return $atts;
  510. }
  511. /**
  512. * Combines user attributes with known attributes and fill in defaults when needed.
  513. *
  514. * The pairs should be considered to be all of the attributes which are
  515. * supported by the caller and given as a list. The returned attributes will
  516. * only contain the attributes in the $pairs list.
  517. *
  518. * If the $atts list has unsupported attributes, then they will be ignored and
  519. * removed from the final returned list.
  520. *
  521. * @since 2.5.0
  522. *
  523. * @param array $pairs Entire list of supported attributes and their defaults.
  524. * @param array $atts User defined attributes in shortcode tag.
  525. * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
  526. * @return array Combined and filtered attribute list.
  527. */
  528. function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
  529. $atts = (array) $atts;
  530. $out = array();
  531. foreach ( $pairs as $name => $default ) {
  532. if ( array_key_exists( $name, $atts ) ) {
  533. $out[ $name ] = $atts[ $name ];
  534. } else {
  535. $out[ $name ] = $default;
  536. }
  537. }
  538. if ( $shortcode ) {
  539. /**
  540. * Filters shortcode attributes.
  541. *
  542. * If the third parameter of the shortcode_atts() function is present then this filter is available.
  543. * The third parameter, $shortcode, is the name of the shortcode.
  544. *
  545. * @since 3.6.0
  546. * @since 4.4.0 Added the `$shortcode` parameter.
  547. *
  548. * @param array $out The output array of shortcode attributes.
  549. * @param array $pairs The supported attributes and their defaults.
  550. * @param array $atts The user defined shortcode attributes.
  551. * @param string $shortcode The shortcode name.
  552. */
  553. $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
  554. }
  555. return $out;
  556. }
  557. /**
  558. * Removes all shortcode tags from the given content.
  559. *
  560. * @since 2.5.0
  561. *
  562. * @global array $shortcode_tags
  563. *
  564. * @param string $content Content to remove shortcode tags.
  565. * @return string Content without shortcode tags.
  566. */
  567. function strip_shortcodes( $content ) {
  568. global $shortcode_tags;
  569. if ( false === strpos( $content, '[' ) ) {
  570. return $content;
  571. }
  572. if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
  573. return $content;
  574. }
  575. // Find all registered tag names in $content.
  576. preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
  577. $tags_to_remove = array_keys( $shortcode_tags );
  578. /**
  579. * Filters the list of shortcode tags to remove from the content.
  580. *
  581. * @since 4.7.0
  582. *
  583. * @param array $tags_to_remove Array of shortcode tags to remove.
  584. * @param string $content Content shortcodes are being removed from.
  585. */
  586. $tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content );
  587. $tagnames = array_intersect( $tags_to_remove, $matches[1] );
  588. if ( empty( $tagnames ) ) {
  589. return $content;
  590. }
  591. $content = do_shortcodes_in_html_tags( $content, true, $tagnames );
  592. $pattern = get_shortcode_regex( $tagnames );
  593. $content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );
  594. // Always restore square braces so we don't break things like <!--[if IE ]>.
  595. $content = unescape_invalid_shortcodes( $content );
  596. return $content;
  597. }
  598. /**
  599. * Strips a shortcode tag based on RegEx matches against post content.
  600. *
  601. * @since 3.3.0
  602. *
  603. * @param array $m RegEx matches against post content.
  604. * @return string|false The content stripped of the tag, otherwise false.
  605. */
  606. function strip_shortcode_tag( $m ) {
  607. // Allow [[foo]] syntax for escaping a tag.
  608. if ( '[' === $m[1] && ']' === $m[6] ) {
  609. return substr( $m[0], 1, -1 );
  610. }
  611. return $m[1] . $m[6];
  612. }