I need a list of every shortcode inside the content. Is there any way to list them?
This is what I need:
$str="[term value="Value" id="600"][term value="Term" id="609"]";
So every shortcode should be inside the $str
.
I found a code snippet to check if there is a shortcode. But how can I display them all?
$content="This is some text, (perhaps pulled via $post->post_content). It has a shortcode.";
if( has_shortcode( $content, 'gallery' ) ) {
// The content has a short code, so this check returned true.
}
3 Answers
Here’s one way:
You can look at has_shortcode() and find the parsing there:
preg_match_all(
"https://wordpress.stackexchange.com/" . get_shortcode_regex() . "https://wordpress.stackexchange.com/",
$content,
$matches,
PREG_SET_ORDER
);
using the get_shortcode_regex() function for the regex pattern.
For non empty matches, you can then loop through them and collect the full shortcode matches with:
$shortcodes = [];
foreach( $matches as $shortcode ) {
$shortcodes[] = $shortcode[0];
}
Finally you format the output to your needs, e.g.:
echo join( '', $shortcodes );
PS: It can be handy to wrap this into your custom function.