0

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.)

1 Answer 1

1

You can do that, but you shouldn’t:

  1. Keep the global namespace clean. Each name in the global namespace is a collision candidate. Making a variable global, a constant or a function increases the likelihood of a collision, no matter how good your prefixes are.

  2. Constants are the slowest global types. Yes, that’s micro-optimization, but why introducing it when there are better options?

  3. Constants are not flexible. Imagine you change your shortcode callback to accept two parameters. Or five. Or ten. Your code will become very ugly very fast.

Alternatives

Provide the defaults in a separate function:

function my_plugin_prefix_get_default_shortcode_attributes()
{
    return array (
        'numitems' => 5
    );
}

Now you can access these defaults from any place, and you can extend the array easily.

In your shortcode:

$attribute = shortcode_atts( 
    my_plugin_prefix_get_default_shortcode_attributes(),
    $atts
);

Move the function into your plugin’s main class or into a separate class:

class My_Plugin
{
    public static function get_default_shortcode_attributes()
    {
        return array (
            'numitems' => 5
        );
    }
}

In your shortcode:

$attribute = shortcode_atts( 
    My_Plugin::function get_default_shortcode_attributes(),
    $atts
);
4
  • Great idea, can't believe I didn't think of that. Thank you!
    – JimmyPena
    Mar 18, 2013 at 13:45
  • Sorry, I have to ask. What I am trying to do is avoid having to hardcode variable names in my shortcode. Is there any way to derive the variable name from the constant? IOW, can I derive $numitems from DEFAULT_NUM_NAME? Thx
    – JimmyPena
    Apr 2, 2013 at 16:21
  • @JimmyPena You have to write down the attribute keys somewhere, in this case in get_default_shortcode_attributes(). A constant is not needed.
    – fuxia
    Apr 2, 2013 at 22:36
  • I think I may have found the answer. If I read the value of the constant into an intermediate variable using the constant function, I can read that variable's value by adding another $ in front of a variable name.
    – JimmyPena
    Apr 3, 2013 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.