0

I'm trying to write my first caption plugin and having some difficulties.

I'm setting up a shortcode like so:

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

   // DEFAULT ARGUMENTS ARRAY       
   $args=shortcode_atts( array(
    'caption' => 'Caption',
    'link' => 'http://www.link.com'
   ), $atts);

   // ENCLOSED SHORTCODES
   if($content){
      return '<div class="Container-Caption" 
        alt="'. $args['caption'] .'" 
        rel="'. $args['link'] .'">
          '.$content.'</div>';
   };
};  

add_shortcode( 'Caption', 'Shortcode_Caption' );

Some JS picks it up from there. So then when the shortcode is used:

[Caption caption="This is the Text" link="http://www.go_here.com"]some content[/Caption]

The above doesn't work, because of the spaces in the "caption" attribute. However, removing the spaces works 100%:

[Caption caption="Text" link="http://www.go_here.com"]some content[/Caption]

I'm not sure what I'm doing wrong with this? Thanks!

4
  • Your code is outputting strangely formatted markup but the spaces don't seem to matter. What doesn't work? The Javascript?
    – s_ha_dum
    Mar 25, 2014 at 2:36
  • Did you end up finding an answer to this, agreed the HTML markup isn't great but i'm having the same issue of spaces in attributes causing the "shortcode_atts" function to use the default values
    – csilk
    Sep 9, 2014 at 0:18
  • No, no resolution - sorry
    – Aaron
    Sep 12, 2014 at 21:20
  • I was having the same issue and abandoned shortcode_parse_atts() to use a regex solution. I found the solution on this thread: stackoverflow.com/questions/22592608/…
    – Nick
    May 11, 2017 at 1:14

1 Answer 1

0

The way your code is written, you are breaking your div tag over several lines-- literally like this, if you look at the generated source:

<div class="Container-Caption" 
        alt="Oh I have been to Ludlow Fair" 
        rel="http://www.link.com">
          blah</div>

I do not see any different between using a caption with or without spaces, but breaking your tag like that is non-standard, if not actually wrong ( and it doesn't seem to actually be wrong based on some very quick research). It does look like the kind of thing that Javascript could choke on though. I'd suggest rewriting that so that it generates a bit more typical markup.

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

   // DEFAULT ARGUMENTS ARRAY       
   $args=shortcode_atts( array(
    'caption' => 'Caption',
    'link' => 'http://www.link.com'
   ), $atts);

   // ENCLOSED SHORTCODES
   if($content){
      $ret = '<div class="Container-Caption"'; 
      $ret .= ' alt="'. $args['caption'] .'"';
      $ret .= ' rel="'. $args['link'] .'">';
      $ret .=  $content.'</div>';
      return $ret;
   };
};  

add_shortcode( 'mycaption', 'Shortcode_Caption' );

echo do_shortcode('[mycaption caption="Oh I have been to Ludlow Fair" ]blah[/mycaption]');

Also note that you are breaking a rule specifically spelled out in the Codex:

Shortcode names should be all lowercase and use all letters, but numbers and underscores should work fine too. Be wary of using hyphens (dashes), you'll be better off not using them.

http://codex.wordpress.org/Shortcode_API

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.