0

Hope someone can help me with this.

This is my code:

function swh_woocommerce_store_credit_shortcode() {
    $store_credits = swh_woocommerce_get_store_credits();
    echo "<span style= 'display: inline;'>Remaining Credit:</span>";
    return wc_price($store_credits);
    
}
add_shortcode( 'swh_store_credit_total_value', 'swh_woocommerce_store_credit_shortcode' );

I'm trying to display echo and return in the same line.

i. e. Remaning Credit: $80 and not

Remaining Credit:

$80

Thanks!

0

You must not use use echo or print in a shortcode function. The echoed string will be printed out immediately when the content is processed, which is almost always too early.

So in your function you should just return the string without using echo:

function swh_woocommerce_store_credit_shortcode() {
    $store_credits = swh_woocommerce_get_store_credits();
    return "<span style= 'display: inline;'>Remaining Credit:</span>" . wc_price($store_credits);    
}
1
  • It worked great! Thank you so much!
    – Gumo
    Aug 27 at 22:40
0
function swh_woocommerce_store_credit_shortcode() {
    $store_credits = swh_woocommerce_get_store_credits();
    $price = wc_price($store_credits);
    echo "<span style= 'display: inline;'>Remaining Credit: {$price}</span>";
    return $price;
    
}
add_shortcode( 'swh_store_credit_total_value', 'swh_woocommerce_store_credit_shortcode' );

But be careful what you want to return, wp_price or a HTML :)

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.