In my plugin I define a constant as follows:
define('DEFAULT_NUM', 5);
I also define a constant representing the name by which I want to refer to this number:
define('DEFAULT_NUM_NAME', 'numitems');
My plugin has a shortcode that uses these values as follows:
function do_something($atts) {
extract( shortcode_atts( array(
DEFAULT_NUM_NAME => DEFAULT_NUM
), $atts ) );
I do this so I can change variable values in one place, instead of hardcoding them:
function do_something($atts) {
extract( shortcode_atts( array(
'numitems' => 5
), $atts ) );
The extract
function will convert the string 'numitems' into a variable called $numitems
. So the rest of my code has to look like this:
if ($numitems == 5) {
return 'blah blah';
}
In effect, I am hardcoding the variable's name because I have to use whatever name I defined. I would like to retrieve the value of the variable without having to use it.
In other words, I want to leverage the constant name to determine the value passed to the plugin, without having to use the variable.
Is this possible? Or is my approach wrong?
(FYI This is a test case based on an actual plugin. These are not the actual variable names.)