$atts passed to $callback of add_shortcode() is not always an array
-
According to https://developer.wordpress.org/reference/functions/add_shortcode/:
$callback callable Required
The callback function to run when the shortcode is found.
Every shortcode callback is passed three parameters by default, including an array of attributes ($atts), the shortcode content or null if not set ($content), and finally the shortcode tag itself ($shortcode_tag), in that order.However, this part
including an array of attributes ($atts)
is not always true:
if the shortcode is used without any attributes, then$atts
is an empty string.This is also “proven” by looking at https://developer.wordpress.org/reference/functions/shortcode_atts/ source code which actually typcasts $atts to array. This would not be necessary, would $atts indeed be always an array.
Now, this causes issues in a more complex programming scenario where we assume $atts to be an array (as stated by the documentation). I will not delve into details, but it is related to type hinting in methods, and also when you want to validate the $atts before it gets sent to shortcode_atts
(this might be unconventional, but it is my choice to NOT process the $atts before they are not validated and sanitised (I am aware I could do that after shortcode_atts has merged the atts…).
We’ve to do cumbersome checks or type cast $atts… which really should not be necessary. And type hinting still would not be possible in our callback.So I wanted to at least get a discussion going on trac, because IMO it should be ensured that $atts passed to $callback is always an array – empty or not.
Yet, to do that, I wanted to do some debug first and… I just can’t figure out where $atts is actually passed to the callback, respectively where it is defined and populated, in the source code.Can anyone point me to where this is handled?
I would imagine all it need is a type cast on that line of code that actually populates and then passes $atts to the callback.- This topic was modified 9 months, 4 weeks ago by .
- This topic was modified 9 months, 4 weeks ago by . Reason: Darn autocorrect
The page I need help with: [log in to see the link]
- The topic ‘$atts passed to $callback of add_shortcode() is not always an array’ is closed to new replies.