skip to Main Content

Hello can you help me,

i want to add shortcode after this <div class="customads1" and <div class="customads2"

this is the complete code i added in the function.php

add_action( 'storefront_footer', 'fuyn_footer_widgets', 11 );
function fuyn_footer_widgets(){
    if( is_checkout() ){
 global $woocommerce ; 

if ( $woocommerce->cart->total != 0 ) {
    return;
}
echo '<div class="customads" style="width: 100%;display: flex;">
    <div class="customads1" style="width: 50%;background: bisque;margin: 5px;">Shortcode.</div>
    <div class="customads2" style="width: 50%;background: bisque;margin: 5px;">Shortcode.</div>
</div>';
}
}

how to do it,

this is my short code

[sc name="ads"][/sc]

Thank you

2

Answers


  1. You can use do_shortcode()

    add_action( 'storefront_footer', 'fuyn_footer_widgets', 11 );
    function fuyn_footer_widgets(){
        if( is_checkout() ){
     global $woocommerce ; 
    
    if ( $woocommerce->cart->total != 0 ) {
        return;
    }
    echo '<div class="customads" style="width: 100%;display: flex;">
        <div class="customads1" style="width: 50%;background: bisque;margin: 5px;">Shortcode.</div> ' . do_shortcode( '[sc name="ads"]' ) . '
        <div class="customads2" style="width: 50%;background: bisque;margin: 5px;">Shortcode.</div>
    </div>';
    }
    }
    
    Login or Signup to reply.
  2. To add a shortcode inside a PHP code on WordPress. You need to use do_shortcode() like this:

    add_action( 'storefront_footer', 'fuyn_footer_widgets', 11 );
    function fuyn_footer_widgets(){
        if( is_checkout() ){
     global $woocommerce ; 
    
    if ( $woocommerce->cart->total != 0 ) {
        return;
    }
    echo '<div class="customads" style="width: 100%;display: flex;">
        <div class="customads1" style="width: 50%;background: bisque;margin: 5px;">'.do_shortcode('[sc name="ads"]').'</div>
        <div class="customads2" style="width: 50%;background: bisque;margin: 5px;">'.do_shortcode('[sc name="ads"]').'</div>
    </div>';
        }
    }
    

    You can also structure it better like this:

    add_action( 'storefront_footer', 'fuyn_footer_widgets', 11 );
    function fuyn_footer_widgets(){
        if( is_checkout() ){
     global $woocommerce ; 
    
    if ( $woocommerce->cart->total != 0 ) {
        return;
    }
    $shortcodeValue = do_shortcode('[sc name="ads"]');
    ?>
    <div class="customads" style="width: 100%;display: flex;">
        <div class="customads1" style="width: 50%;background: bisque;margin: 5px;">
       <?php echo $shortcodeValue; ?>
       </div>
        <div class="customads2" style="width: 50%;background: bisque;margin: 5px;"> 
           <?php echo $shortcodeValue;?> 
        </div>
    </div>
    <?php } } ?>
    

    OR

    add_action( 'storefront_footer', 'fuyn_footer_widgets', 11 );
    function fuyn_footer_widgets(){
        if( is_checkout() ){
     global $woocommerce ; 
    
    if ( $woocommerce->cart->total != 0 ) {
        return;
    }
    $shortcodeValue = do_shortcode('[sc name="ads"]');
    ?>
    echo '<div class="customads" style="width: 100%;display: flex;">
        <div class="customads1" style="width: 50%;background: bisque;margin: 5px;">'.$shortcodeValue.'</div>
        <div class="customads2" style="width: 50%;background: bisque;margin: 5px;">'.$shortcodeValue.'</div>
    </div>';
    }
    }
    

    Note: do_shortcode() always requires an echo.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search