0

Is it allowed to add an attribute to a shortcode without giving it a value? something like:

[shortcode attr1="hasvalue" attr2]

The attribute attr1 has a value, which I can easily catch in a function like: function shortcode( $atts ) {... with $atts['attr1'].

Can I also catch the other attribute attr2? Its value doesn't exist, so $atts['attr2'] is undefined/empty. But can I maybe simply detect if this attribute has been written or not?

This would be useful for binary settings, such as a true or false indication, where it is a bit redundant to write true="true" or something like that.

3
  • 1
    Sorta? Check out the discussion here: https://wordpress.stackexchange.com/questions/10973/shortcode-empty-attribute kingjeffery's answer (which is looping through the $atts array and checking for any attribute with the name) sounds like it might be a fit. Commented Sep 19, 2019 at 19:14
  • 1
    I have flagged my question as a duplicate of: Shortcode empty attribute
    – Steeven
    Commented Sep 20, 2019 at 12:05
  • @SeanVõKirkpatrick Thank you for the link. This seems to answer my questions. I have marked my question as a duplicate of: Shortcode empty attribute. It seems to be possible to have an attribute with no value, but it is not fool-proof since when you loop after the term attr2, it would also fire true if this term was used within another already present attribute.
    – Steeven
    Commented Sep 20, 2019 at 12:07

0

Your Answer

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

Browse other questions tagged or ask your own question.