Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Last active December 29, 2015 13:29
Show Gist options
  • Save hereswhatidid/7677861 to your computer and use it in GitHub Desktop.
Save hereswhatidid/7677861 to your computer and use it in GitHub Desktop.
Detect the existence of multiple shortcodes at one time via an array of strings within WordPress.
<?php
/**
* Loop through an array of shortcodes and check for their existence
* @author Gabe Shackle <gabe@hereswhatidid.com>
* @param array $shortcodes An array of shortcode strings
* @param boolean $match_all Whether to check for the existence of ALL the shortcodes, or just ANY of them
* @return mixed The results will either be an array of the found shortcodes or boolean false
*/
function shortcodes_exist( $shortcodes = array(), $match_all = false ) {
$found_shortcodes = array();
foreach( $shortcodes as $shortcode ) {
if ( shortcode_exists( $shortcode ) ) {
$found_shortcodes[] = $shortcode;
} else {
if ( $match_all ) {
return false;
}
}
}
if ( count( $found_shortcodes ) === 0 ) {
return false;
} else {
return $found_shortcodes;
}
}
$shortcodes_to_find = array(
'gallery',
'audio',
'thiswontwork'
);
$find_any = shortcodes_exist( $shortcodes_to_find );
// returns array( 'gallery', 'audio' );
$find_all = shortcodes_exist( $shortcodes_to_find, true );
// returns false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment