Skip to content

Instantly share code, notes, and snippets.

@skoskie
Last active March 4, 2019 02:42
Show Gist options
  • Save skoskie/9442291 to your computer and use it in GitHub Desktop.
Save skoskie/9442291 to your computer and use it in GitHub Desktop.
In Wordpress, this function will get a list of all parameters defined in all functions that are used as shortcodes. Why anyone would ever need something like this, I have no idea.
<?php
/**
* Gets all arguments for all shortcode functions and returns them as an array.
*
* @method get_all_shortcode_args
*
* @param boolean $ignore_native Whether to ignore the native Wordpress shortcodes.
*
* @param boolean $dedupe Whether to return unique values or allow duplicates.
*
* @return array A list of shortcode argument names as strings, without the dollar sign prefix.
*/
function get_all_shortcode_args($ignore_native = TRUE, $dedupe = FALSE)
{
global $shortcode_tags;
// What if I told you there were no shortcode tags?
// What if I told you this isn't even Wordpress?
if(!isset($shortcode_tags) || !$shortcode_tags) return array();
// Define ignored shortcodes as array keys. Values don't matter.
$known_shortcode_tags = array(
'embed' => null, // Native WP Shortcode
'wp_caption' => null, // Native WP Shortcode
'caption' => null, // Native WP Shortcode
'gallery' => null, // Native WP Shortcode
'audio' => null, // Native WP Shortcode
'video' => null, // Native WP Shortcode
'hightouch_sct_1' => null // Ex. Your own shortcodes that you want to ignore.
);
// Now we get a list of shortcodes that want to act on, optionally filtered.
$my_shortcode_list = ( $ignore_native ? array_diff_key($shortcode_tags, $known_shortcode_tags) : $shortcode_tags );
/**
* Set up the array that will hold the args.
* As of now, this will hold every arg for every shortcode function
* I don't know what they fuck you want to do with the info, so this
* is how the data is returned for now.
*/
$all_args = array();
foreach ($my_shortcode_list as $tag => $fn_name)
{
// The magic. Reflection API allows you to reverse engineer the function.
$ref_fn = new ReflectionFunction($fn_name);
/**
* The getParameters() method returns a ReflectionParameter object
*
* @link http://www.php.net/manual/en/reflectionfunctionabstract.getparameters.php
*/
foreach ($ref_fn->getParameters() as $param)
{
// Oddly, I think name is the only property.
array_push($all_args, $param->name);
}
}
// Return the results, optionally deduped.
return ($dedupe ? array_unique($all_args, SORT_STRING) : $all_args );
/* Should look something like this if dedupe === false:
Array
(
[0] => atts
[1] => atts
[2] => content
[3] => code
[4] => atts
[5] => content
[6] => code
)
*/
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment