您还可以使用WordPress本身提供的正则表达式get_shortcode_regex
:
function wpse250308_get_questions() {
global $post;
if ( preg_match_all(get_shortcode_regex(\'question\'), $post->post_content, $questions ) ) {
$questions = array_key_exists( 1 , $questions) ? $questions[1] : array();
// $questions will contain the array of the question shortcodes
// but also, get_shortcode_regex returns a regex that provides some useful capture groups:
// the get_shortcode_regex from WP provides the matches:
// 1 – An extra [ to allow for escaping shortcodes with double [[]]
// 2 – The shortcode name
// 3 – The shortcode argument list
// 4 – The self closing /
// 5 – The content of a shortcode when it wraps some content.
// 6 – An extra ] to allow for escaping shortcodes with double [[]]
//Do your stuff
}
}