1

question seems to be similar to the one asked here but the answer went down a different path.

Pretty much if a user enters a shortcode attribute with a space it will get overwritten by the default value and I can't figure out how to stop this behavior (no mention in the WP Shortcode API docs)

Shorcode:

[imageaccordion title="We're awesome"]Content Here[/imageaccordion]

PHP:

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

    // Attributes

    extract(
        shortcode_atts(
            array(
                'title' => 'Accordion Title',
            ), $atts , 'imageaccordion'
        )
    );

    // Put It Together

    ob_start();
    ?>
    <div class='image-accordion'>
          <h2>
            <?php echo $title; ?>
          </h2>
    <?php

    $outputbefore = ob_get_clean();
    $outputafter = '</div>';

    //Return 

    return $outputbefore . do_shortcode($content) . $outputafter;
}

add_shortcode( 'imageaccordion', 'image_accordion' );

I seem to get an output of

<div class="image-accordion'>
  <h2>
     Accordion Title
  </h2>
</div>

Every time...

If I run a print_r on $atts I get

Array ( [0] => 
/> [2] => title="We're [3] => Awesome")

This is on a new installation of WP, no plugins running with the default theme

(have cut down the eg. code too)

  • 1
    There's nothing special that WP does with attributes containing spaces - just tried your shortcode and it works fine - can only think $title is getting overwritten in code between the extract and echo that isn't shown... – bonger Sep 9 '14 at 5:26
  • Hmm, i've posted the full code here: chopapp.com/#lg49uyj3 Nope, nothing that overwriting the variable... Seems to happen on all vars with spaces too, can't figure it out – csilk Sep 9 '14 at 5:46
  • 1
    Again worked! However, if you're calling it like in the usage example with newlines between attributes then WP won't like that ... try one line [imageaccordion title="We're Value" hovertext="Blah blah hover"] – bonger Sep 9 '14 at 6:07
  • Yep, looks like the multiline was the problem... Thanks for checking it out :) – csilk Sep 9 '14 at 6:26
1

Looks like I was multi-lining the shortcode itself, wordpress wasn't a fan.

Thanks @bonger

| improve this answer | |
  • Actually, looking at the shortcode_parse_atts code, it'd be an easy fix for WP to deal with this (which it should) by just stripping the <br />'s, eg $text = preg_replace('/<br \/>$/m', '', $text); - might open a ticket on this ... then again, probably won't :( ... – bonger Sep 9 '14 at 6:50
0

The problem seems to be in output buffering. Why not just use assignment operators?

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

    // Attributes
    $args = shortcode_atts( array(
        'title' => 'Accordion Title',
    ), $atts , 'imageaccordion' );

    $output = '<div class="image-accordion">';
        $output .= '<h2>' . $args['title'] . '</h2>';
    $output .= '</div>';

    $output .= do_shortcode($content);

    return $output;
}
add_shortcode( 'imageaccordion', 'image_accordion' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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