1

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 ?

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.