Support » Fixing WordPress » Number sign causes syntax error in shortcode function

  • Resolved Tanath Fedo

    (@tanath-fedo)


    Hi, all. I can’t figure out why I can’t use the number sign (#) inside a shortcode function (for hex color).
    Here’s the code:

    function box_shortcode( $atts, $content = null ) {
    extract(shortcode_atts(array(
    'color' => white,
    'background' => red,
    'border' => gray,
    'width' => 200,
    'float' => none
    ), $atts));
    return '<div class="shoutbox" style="color: ' . $color . ';
    background: ' . $background . '; border-color: ' . $border . '; width: ' . $width . 'px;
    float: ' .$float . ';">' . $content . '</div>';
    }
    add_shortcode( 'box', 'box_shortcode' );

    If I try to use a hex color value, like “#888”, instead of the word “gray” I get the error, and WP disables the plugin.
    The error says: Parse error: syntax error, unexpected T_DOUBLE_ARROW, expecting ‘)’

Viewing 5 replies - 1 through 5 (of 5 total)
  • That error usually comes from code that doesn’t close a parenthesis or quote correctly. Since you’re doing a hex value you need to wrap it in single quotes:

    function box_shortcode( $atts, $content = null ) {
    extract(shortcode_atts(array(
    'color' => white,
    'background' => red,
    'border' => '#888',
    'width' => 200,
    'float' => none
    ), $atts));
    return '<div class="shoutbox" style="color: ' . $color . ';
    background: ' . $background . '; border-color: ' . $border . '; width: ' . $width . 'px;
    float: ' .$float . ';">' . $content . '</div>';
    }
    add_shortcode( 'box', 'box_shortcode' );

    See if that’ll work.

    Thread Starter Tanath Fedo

    (@tanath-fedo)

    Thanks, that did it.

    I wonder why the standard then isn’t just to always have the single quotes surrounding those attributes?

    Kinda weird that suddenly you need them if you use a “#”. I wonder if any other characters will make it kick out an error?

    I think white, red, and gray possibly made it through because they’re known values to HTML. But that’s a good question.

    It is safer to put quotes around all of the array values like “red” and “none” too — other words could cause errors similar to the one with the ‘#’. (You should get a similar error if you used a word that means something in php like “return” in an array value instead of “red”, or something with symbols and spaces like “linear-gradient(#fff, #ddf)” without quotes.)

    If PHP was in strict mode, I think you should get a notice about the strings that are not quoted. (If you can find a file named “error_log” in the folder you’re working in, your site’s root, or wherever your web server’s access log is, you may see the notice in there.)

    The number “200” should work either way, but if you wanted a number like 200.0, to appear exactly as written in the output, it would need to be quoted too.

    Thread Starter Tanath Fedo

    (@tanath-fedo)

    Great! Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Number sign causes syntax error in shortcode function’ is closed to new replies.