How to find specific shortcodes in a post or in a widget and pass them to a variable?

I’ve created a shortcode in my plugin and I used it in some pages. Now I have to get the attributes of these shortcodes on the posts that includes the shortcode.

A sample shortcode is [book id=”432″]. I have to get this 432 value when I write this shortcode into a post or a widget.

I see this function in order to get an attribute from a shortcode string:

function get_shortcode_atts( $tag, $text ){
preg_match_all( ‘/’ . get_shortcode_regex() . ‘/s’, $text, $matches );
$out = array();
if( isset( $matches[2] ) )
{
foreach( (array) $matches[2] as $key => $value )
{
if( $tag === $value )
$out[] = shortcode_parse_atts( $matches[3][$key] );
}
}
return $out;
}

It’s ok. But before this what should I do, in order to detect a shortcode and get it from a post or i widget?

There is a function named has_shortcode but I think it’s not enough for my purpose.

Read more here:: How to find specific shortcodes in a post or in a widget and pass them to a variable?

Leave a Reply

Your email address will not be published. Required fields are marked *