Skip to content

Instantly share code, notes, and snippets.

@ivorpad
Created October 29, 2016 20:48
Show Gist options
  • Save ivorpad/6f229e85fcee53beca7acf8480b1c3d6 to your computer and use it in GitHub Desktop.
Save ivorpad/6f229e85fcee53beca7acf8480b1c3d6 to your computer and use it in GitHub Desktop.
Grab all attributes for a given shortcode in a text
<?php
/**
* Grab all attributes for a given shortcode in a text
*
* @uses get_shortcode_regex()
* @uses shortcode_parse_atts()
* @param string $tag Shortcode tag
* @param string $text Text containing shortcodes
* @return array $out Array of attributes
*/
// This function is to get attributes from [shortcode attr="val"]
// Usage: $get_title = inoic_get_child_sc_attr('shortcode tag', $content);
function prefix_get_child_sc_attr( $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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment