XanderMan
XanderMan

Reputation: 143

Wordpress: use PHP script to insert shortcode into div with class

I have this PHP-script that ads a shortcode at the top of my wooCommerce shop-page.

add_action( 'woocommerce_archive_description',
     'woocommerce_archive_description', 15 );
function woocommerce_archive_description() {
    echo do_shortcode("[my-shortcode]");
}

It works as I expect it to. But if I'd want the shortcode to appear within a certain div with a certain class - or even better: Append it after a certain element with a class.

This code is auto generated:

   <button class="button" data-selector="astra-off-canvas-sidebar-wrapper"><span class="astra-woo-filter-icon"></span><span class="astra-woo-filter-text">PRODUKTSØK</span></button> 

So Preferably I would love to append the shortcode to the page, after the above button.

How would you go about that ?

Upvotes: 1

Views: 1792

Answers (1)

gaurav sharma
gaurav sharma

Reputation: 544

Try this

Step 1: woocommerce shop page hook

add_action( 'woocommerce_archive_description',
     'woocommerce_archive_description', 15 );
function woocommerce_archive_description() {

    echo do_shortcode("[my-shortcode]");

    echo '<button class="button" data-selector="astra-off-canvas-sidebar-wrapper"><span class="astra-woo-filter-icon"></span><span class="astra-woo-filter-text">PRODUKTSØK</span></button>'; 
}

Step 1: also, have added custom shortcode

function my_shortcode_function(){

    ob_start();

    ?>
    <div class="my-shortcode-container">
        Yor shortcode body
    </div>

    <script type="text/javascript">
        jQuery(document).ready(function(){ //alert('check');

            jQuery('.my-shortcode-container').insertAfter('.button');

        });
    </script>
    <?php
    return ob_get_clean();

}
add_shortcode('my-shortcode','my_shortcode_function');

Here I am using jquery to display shortcode after button.

Upvotes: 2

Related Questions