1

I'm having a problem with shortcodes on a website, currently you enter a shortcode in the post edit screen like the following:

[tab name=Itinerary]

This will output a tab such as:

enter image description here

However as you can see in the image above we get "travel" and "useful" when using the shortcodes below:

[tab name=Travel Info] or [tab name=Useful Info] 

So the problem seems to be in the whitespace and the formatting. I've tried adding a "&/nbsp;" or [tab name="Travel Info"] but these solutions quickly fail. This is the function in functions.php that seems to be taking the information from the post screen:

// Tab Shortcode, creates a tab of information:
// [tab name=tab_name]
function page_tab_func( $atts ) {
    $a = shortcode_atts( array(
        'name' => 'Untitled'
    ), $atts );

    $spec_chars = array("/", "@", "#", " ");
    $sanitised = str_replace($spec_chars, "_", $a['name']);

    // $a['name']; // Gets the tab name:
    $output = "";
    $output .= "</div>";
    $output .= "<div id='content_{$sanitised}' class='tab-section'>";
    $output .= "<h2 class='tab-name'>{$a['name']}</h2>";
    return $output;
}

add_shortcode( 'tab', 'page_tab_func' );
add_filter( 'no_texturize_shortcodes', 'ignore_tag_a' );

    function ignore_tag_a( $list ) {
      $list[] = 'tab';
      return $list;
    }

I've searched Google and spent some time trying to solve this problem, if anyone has any advice in dealing with shortcode formatting or fixing this problem I'd greatly appreciate it.

1 Answer 1

0

You should give the Attributes Section of the Shortcode API WordPress Codex page a read.

Unquoted values also must never contain spaces.

Short summary, use quotes - either " or ' - for attribute values with a space in it. Because an attribute name isn't actually needed I think the »Info« part of your name will be an extra part of your $atts array, stored with a numeric index, so the array likely looks somewhat like this: array( 'name' => 'Travel', 0 => 'Info' ).

Before you say, but I tried quotes, let me say, there is no reason visible why it wouldn't work with using quotes around the value.

1
  • Thanks @ialocin, you mentioned that quotes should work here so I took a look at the problem again and here's what I found: The shortcode was outputting the html as: <div class="content_Useful Info">Useful Info</div> The space in the class name was causing the tabbed title to display correctly but the related info wasn't showing. In the functions I had this code looking for special characters: $spec_chars = array("/", "@", "#", "&nbsp;"); So I took a punt and changed it to this: $spec_chars = array("/", "@", "#", "&nbsp;", " "); Adding the " " seemed to do the trick. Thanks! Dec 19, 2014 at 10:37

Your Answer

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

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