Ei kuvausta

shortcodes.php 21KB

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