I Have a shortcode in post content:
[restrict paid="true"][download id='1234'][/restrict]
I need to extract 1234 value from the shortcode.
I use this funtion but it not work:
function get_download_ids( $content, $shortcode ) {
$pattern = get_shortcode_regex();
$ids = array();
// Find download shortcodes.
if ( preg_match_all( '/'. $pattern .'/s', $content, $matches ) &&
array_key_exists( 2, $matches ) &&
in_array( $shortcode, $matches[2] ) )
{
foreach( $matches[2] as $shortcode_index => $shortcode_name ) {
if ( $shortcode_name === $shortcode ) {
$shortcode_str = $matches[0][ $shortcode_index ];
// Extract shortcode attributes.
$shortcode_attrs_str = str_replace( $shortcode, '', $shortcode_str );
$shortcode_attrs_str = trim( $shortcode_attrs_str, '[]' );
// Convert attributes into array.
$atts = shortcode_parse_atts( $shortcode_attrs_str );
if ( ! empty( $atts['id'] ) ) {
$ids[] = $atts['id'];
}
}
}
}
return array_unique( $ids );
}
Look forward to the help of everyone.
Thank you