0
votes

I get an error from this function when we request this shortcode

function salex_func( $atts ){
   global $product;
if($product->is_on_sale()){
        echo '<span class="onsale soldout">';
    echo __( 'SALE!!!!', 'hello');
    echo '</span>';
}   
}
add_shortcode('saletex', 'salex_func');
2
Can you send me error? - vadivel a
I am not sure about your code as I am not on pc right now but for wp general tip for short code I see issue that you are echoing the output which will make it display stuff at top and not where you have put the shortcode. For that I suggest before first echo use ob_start(); then after the last echo line have it say : return ob_get_clean(); this way it will pick all the buffer output from top and return to shortcode function and that will show it on right place. - Mohsin

2 Answers

0
votes

You can't echo shortcode output. You have to return it.

function salex_func( $atts ){
   global $product;
    if($product->is_on_sale()){
        $output = '<span class="onsale soldout">';
        $output .= __( 'SALE!!!!', 'hello');
        $output .= '</span>';
     }
    return $output;   
}
add_shortcode('saletex', 'salex_func');
0
votes

You don't need to echo inside the shortcode function.

You can try this code:

function salex_func( $atts ){
    global $product;
    if($product->is_on_sale()){ 
        ob_start(); ?>
        <span class="onsale soldout"><?php __( 'SALE!!!!', 'hello'); ?></span>
        <?php return ob_get_clean();
    }   
}
add_shortcode('saletex', 'salex_func');