Skip to content

Instantly share code, notes, and snippets.

@OriginalEXE
Last active August 29, 2015 13:56
Show Gist options
  • Save OriginalEXE/9051666 to your computer and use it in GitHub Desktop.
Save OriginalEXE/9051666 to your computer and use it in GitHub Desktop.
Retrieve all instances of the specified shortcode (WordPress)
/**
* Retrieve all instances of the specified shortcode
*
* @since Never
*
* @global array $shortcode_tags
* @param string $tag
* @return boolean/array
*/
function oe_filter_shortcodes( $content, $tag, $result = array() ) {
if ( ! empty( $result ) || shortcode_exists( $tag ) ) {
preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) )
return empty( $result ) ? false : $result;
foreach ( $matches as $shortcode ) {
if ( $tag === $shortcode[2] ) {
$result[] = $shortcode[0];
$position = strpos( $content,$shortcode[0] );
if ( $position !== false ) {
$length = strlen( $shortcode[0] );
$content = substr_replace( $content, '', $position, $length );
}
return oe_filter_shortcodes( $content, $tag, $result );
} else if ( ! empty( $shortcode[5] ) ) {
return oe_filter_shortcodes( $shortcode[5], $tag, $result );
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment