echo
is a PHP language construct which pushes values to the output buffer. It does not have a return value, so concatenating it with a string would cause everything after the echo
to immediately be sent to the output buffer, and everything prior to echo
to compose the concatenated string. This is such a misuse of echo
that PHP itself doesn’t actually allow it – if you had WordPress debugging enabled you would see an error similar to
Parse error: syntax error, unexpected ‘echo’ (T_ECHO)
This error is what is causing your white screen – when not in debug mode, WordPress suppresses error output to avoid exposing potentially sensitive information to end-users.
You shouldn’t use echo
in shortcode logic, as internally WordPress does more processing with a shortcode’s return value. So using echo
in a shortcode has a good chance to mess up your final markup.
The inclusion of the echo
before the edd_get_cart_total()
does not result in currency formatting. I’ve dug through the plugin in question’s source code just to be sure. Rather, it’s more likely that some function is hooked to the edd_get_cart_total
filter to format the output in templates (thus formatting the total when you used it in your header.php
template), however within the context of a shortcode that filter is not attached.
Conveniently, the plugin provides the ebb_cart_total()
function which will always produce a currency-formatted total string. The first argument to the function is $echo
which is true by default, and will cause the function to display the total instead of returning it – which, as detailed earlier, is not something you want to do in a shortcode – so set this argument to false
to have the function return a string which you may concatenate with the rest of your shortcode markup.
All together:
function eddminicartfunc() {
return
'<div class="mobilemenucart">
<i class="fa fa-shopping-cart"></i>
<span class="header-cart-total"> ' . edd_cart_total( false ) . ' </span>
<span class="header-cart edd-cart-quantity">' . edd_get_cart_quantity() . '</span>
</div>';
}
add_shortcode( 'eddminicart', 'eddminicartfunc' );