wpautop() when shortcode attributes are on new lines break args array

I have a custom shortcode tag with a few attributes, and I would like to be able to display its attributes on new lines – to make it more readable to content editors:

[component
    attr1 ="value1"
    attr2 ="value of the second one"
    attr3 ="another"
    attr4 ="value"
    ...
    attrN ="valueN"]

The reason behind this requirement is that a few attributes might be quite verbose in content. Unfortunately, wpautop() adds some nasty extra markup that breaks the $atts array like this (using php print_r($atts)):

Read More
Array ( [0] => attr1 [1] => ="value1"
/> [3] => attr2 = [4] => "value [5] => of [6] => the [7] => second [8] => one"
/> [10] => "" //...and more like this)

I’ve tried with the attributes inline:

[component attr1 ="value1" attr2 ="value of the second one" ="value"... attrN ="valueN"]

and the output is as expected:

Array ( [attr1] => value1 [attr2] => value of the second one [attr3] => //...and so on)

is there any way to have the attributes intented and avoid that extra markup that breaks the $args array?

the function that I use to register the shortcode [component] extracts the values like this:

function component_func( $atts, $content = null ) {
extract( shortcode_atts( array(
    'attr1' => 'attr1',
    'attr2' => 'attr2',
    'attr3' => 'attr3',
    ), $atts ) );
    // more code
}

I then use those extracted variables to populate another array

Related posts

Leave a Reply

1 comment

  1. Ok, I’ve never seen a shortcode done in this way so I threw together a bit of code to test your scenario. And I get the same results with code that works just fine with the standard shortcode format.

    Standard Shortcode Format

    [shortcode name1=”value1″ name2=”value2″]

    I don’t believe this is a result of wpautop(), but a result of the way the shortcode attributes are parsed. Look at the example above and you’ll see that the only unquoted whitespace is used to separate the shortcode and the name=>value pairs (i.e. no spaces between the name and the ‘=’, only one space between each name=>value pair). In this context the <br/> is also considered whitespace.

    I’ve not dug in to the particular internals of shortcode parsing so I can’t offer a suggestion on a a hook or filter that would help. Perhaps another user here has some ideas on that.

    If you must format your shortcode like you have it I think you’re going to have to write custom code to parse the array that’s passed to your shortcode function. All the values are there so you should be able to piece them back together.

    Another option might be to provide a form for the end-user to enter the values then populate the shortcode in your content.

    An update to my answer

    Something about my answer was bugging me so I looked again. And you are correct wpautop() is having an effect on the output of the page. But I don’t think it is the cause of your shortcode problems.