Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way of creating an empty attribute for a shortcode?

Example:

function paragraph_shortcode( $atts, $content = null ) {

   return '<p class="super-p">' . do_shortcode($content) . '</p>';
}

add_shortcode('paragraph', 'paragraph_shortcode'); 

User types

[paragraph] something [/paragraph]

and it shows

<p class="super-p"> something </p>

How to add an empty attribute functionality to my first code? So when user types

[paragraph last] something [/paragraph]

It will output:

<p class="super-p last"> something </p>

I believe adding a:

extract( shortcode_atts( array(
            'last' => '',
        ), $atts ) );

is a good start, but how to check if user used the "last" attribute while it doesn't have a value?

share|improve this question

4 Answers 4

up vote 0 down vote accepted

There could be a couple ways to do this. Unfortunately, I don't think any will result in exactly what you're going for. ( [paragraph last] )

  1. You could just create separate shortcodes for [paragraph_first] [paragraph_last] [paragraph_foobar] that handle $content without needing any attributes

  2. You could set the default value for last to false instead of '', then require users to do [paragraph last=""]content[/paragraph]

  3. You could add a more meaningful attribute such as position which could then be used like [paragraph position="first"] or [paragraph position="last"]

Because of the fact that WordPress discards any atts not given defaults, and the fact that an att without an ="value" is given the default value same as if it's not listed at all, I don't see any way to achieve [paragraph last]

Hopefully one of my 3 workaround will prove useful. Good luck!

share|improve this answer
    
just like i thought. I'm going for the first option then. Thank you :) –  Wordpressor Mar 3 '11 at 0:59

WordPress does not discard attributes without values as is suggested by SethMerrick, but it does not handle it as you would expect. In your example, you would not be able to check isset($atts['last']), but "last" is passed as a value to an unnamed attribute. In this case, this would result in TRUE: ($atts[0] == 'last'). So, you could do a foreach loop to check for any attribute with a value of "last".

share|improve this answer
    
This is how I build shortcodes for clients often. Easy to type, easy to check. –  toscho Mar 23 '12 at 23:20
1  
+1 for actually answering the question. (SethMerrick's workarounds are certainly logical, but miss the functionality described in the question.) @toscho — What code do you use to do this? Any way to share? (Could be posted as a separate answer.) –  supertrue May 30 '13 at 8:25
    
This answer would specifically apply to the example above, but it isn't really a good answer because an implementation of it would fail to distinguish [example last] from [example whatever="last"], since it checks values and ignores indices. –  M Miller Nov 12 '13 at 18:57

When [example attr1="value1" attr2 attr3="value3" attr4] is parsed, the $atts parameter yields:

$atts = array (
    'attr1' => 'value1',
          0 => 'attr2',
    'attr3' => 'value3',
          1 => 'attr4'
);

You can normalize this like so, then you can call shortcode_atts on it.

if (!function_exists('normalize_empty_atts')) {
    function normalize_empty_atts ($atts) {
        foreach ($atts as $attribute => $value) {
            if (is_int($attribute)) {
                $atts[strtolower($value)] = true;
                unset($atts[$attribute]);
            }
        }
        return $atts;
    }
}

function paragraph_shortcode ($atts, $content = null) {
    extract(shortcode_atts(
        array (
            'last' => ''
        ),
        normalize_empty_atts($atts)
    ));

    return '<p class="super-p'
        .($last ? ' last' : '')
        .'">'
        .do_shortcode($content)
        .'</p>';
}

add_shortcode('paragraph', 'paragraph_shortcode');

Other notes:

  • [paragraph last] would make $last = true. [paragraph last="1"] would make $last = '1'.
  • Attributes are case-insensitive, so [paragraph LAST] is equivalent to [paragraph last].
  • Attributes cannot be purely numeric, so the foreach loop I created is safe against integer-indexed attributes.
share|improve this answer
    
+1. Such functionality should really be embedded to the core, so that WP provides attributes in a more consistent way. Well done! –  geomagas Mar 1 at 8:35

I found this : http://richjenks.com/wp-shortcode-attributes-without-values/

CODE

function get_flags($atts) {

    $flags = array();

    if (is_array($atts)) {
        foreach ($atts as $key => $value) {
            if ($value != '' && is_numeric($key)) {
                array_push($flags, $value);
            }
        }
    }

    return $flags;

}

USAGE

$flags = get_flags($atts);
if (in_array('your_flag', $flags) {
    // Flag is present
}
share|improve this answer
1  
thank you for the -1 ! this solution works with less feature and less code. –  benoît Apr 2 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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