0
votes

I would like to use a php variable to input the value as the shortcode name for wordpress.

Basically I have an option where people can create an options and give it a shortcode name.

Something like the following:

Is this possible?

$shortcode_name = "name_here";
function $shortcode_name ($atts) {
// code here
}
add_shortcode ($shortcode_name , $shortcode_name);

Thanks

1
@thebluefox not quite or am I missing something? - Peter Wilkinson

1 Answers

2
votes

In PHP 5.3+ you have closures.

$shortcode_name = 'awesome';
${"{$shortcode_name}_fn"} = function($atts) {
  print('Awesome shortcode');
};
add_shortcode($shortcode_name, ${"{$shortcode_name}_fn"});

You can also have variable variable names, in this case, the variable that contains the function is named: awesome_fn.