1

I'm trying to pass a variable into the add_shortcode function to be used as the shortcode name but it does not seem to be working. Is there a way that I can pass a variable similar to this?

foreach ($files1 as $value) {
    $new_value = substr($value, 0, -4);

    add_shortcode($new_value, function() {
        return '<img src="' . PATH . L_ITEMS . $new_value . '.gif">';
    });

}
8
  • 3
    I'm not sure what you want to achieve and frankly, I honestly think that what you are trying is completely overrated. You somewhere have a big design flaw. If $file1 has 100 values, you are going to create 100 shortcodes which you are going to use how? Oct 30, 2014 at 10:04
  • 2
    Two keywords come to mind: class and use. But I wonder if you really need multiple shortcodes here, why not a single shortcode with an attribute?
    – birgire
    Oct 30, 2014 at 10:04
  • Automatically searching through a directory of images and creating shortcodes based off of the files. The shortcodes are added to a dropdown menu that has been inserted into TinyMCE. Oct 30, 2014 at 10:06
  • 2
    Why then not just simply add the image directly from its name. That is exactly what your shortcode does. This is like visiting your neighbour, then walking all around the block to get to your house Oct 30, 2014 at 10:14
  • 1
    I second @Pieter's first comment. 1. Gigantic design flaw / ill formed architecture. 2. Go with one shortcode with an attribute. Oct 30, 2014 at 10:49

2 Answers 2

5

There are two solutions:

  1. Use the third argument for the shortcode.
  2. Use the use keyword.

Examples

  1. The third parameter for each shortcode callback is the name of the shortcode:
foreach ($files1 as $value) {
    $new_value = substr($value, 0, -4);

    add_shortcode($new_value, function( $attributes, $content, $shortcode ) {
        return '<img src="' . PATH . L_ITEMS . $shortcode . '.gif">';
    });
}
  1. Pass the variable you need to the use part of the lambda function:
foreach ($files1 as $value) {
    $new_value = substr($value, 0, -4);

    add_shortcode($new_value, function() use ( $new_value ) {
        return '<img src="' . PATH . L_ITEMS . $new_value . '.gif">';
    });
}

I think you should register just one shortcode and pass the file as attribute value. That would be faster and easier to debug.

1
  • Thank you for the informative answer, I'd upvote you if I could. Oct 30, 2014 at 11:25
-2

I am not sure your mind but I think parameter $new_value you are using in function add_shortcode is undefined.

5
  • $new_value is define just above the add_shortcode function. I'm having trouble passing the value of $new_value as an argument of add_shortcode though. Oct 30, 2014 at 10:28
  • When printed out $new_value outputs the image name. I'm not understanding how it isn't defined. Oct 30, 2014 at 10:34
  • @taido No it is not, it is defined. Please recheck Oct 30, 2014 at 11:55
  • @user3232282 sorry about my comment, it was directed at taido. Unfortunately my comment to him came in after yours so the confusion was that it was directed at you telling you that your variable is not defined :-) I've corrected it now Oct 30, 2014 at 11:58
  • Ahh okay, that makes sense. I was a bit confused, haha. Oct 30, 2014 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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