1

I'm not sure why I can't get this to work. I'm trying to remove the   that is added inside this shortcode...

[box] Text [/box]

Which results in this HTML output:

<div class="box">&nbsp; Text &nbsp;</div>

I want to remove those spaces. I tried to usr str_replace, but it's not removing the &nbsp :

function infoButton($atts, $content = null) {
     extract( shortcode_atts( array(

    'class' => '',

    ), $atts ));

    $str = '<div class="box ' . $class . '">' . do_shortcode($content) . '</div>';
    $new_str = str_replace('&nbsp;','',$str);

    return $new_str; 

    }
    add_shortcode('box', 'infoButton');
  • 1
    Could you use PHP trim()? – Howdy_McGee Mar 14 '16 at 21:58
  • I just tried return trim($str,"&nbsp;") but no change. – LBF Mar 14 '16 at 22:03
  • 1
    You'd use $str = '<div class="box ' . $class . '">' . do_shortcode( trim( $content ) ) . '</div>'; – frogg3862 Mar 14 '16 at 23:04
0

This could be due to the do_shortcode running through wpautop, see here for details on disabling that: https://stackoverflow.com/questions/5940854/disable-automatic-formatting-inside-wordpress-shortcodes

But as frogg3862 said, what you need to do instead of that is to just trim out the beginning and ending whitespace from $content to prevent the non-breaking space from being automatically added.

function infoButton($atts, $content = null) {
     extract( shortcode_atts( array(

    'class' => '',

    ), $atts ));

    $str = '<div class="box ' . $class . '">' . do_shortcode( trim( $content ) ) . '</div>';

    return $str; 

}
add_shortcode('box', 'infoButton');

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.