user3102556
user3102556

Reputation: 79

woocommerce adding js to product description

I have been asked to add a short JavaScript script to the short description of every product description on a WooCommerce site. It is actually an image with a simple "onclick" that scrolls smoothly to another part of the page, depending on different conditions. It has been added to every product via a PHP script directly to the MySQL DB. It works fine, but the only problem is that, when editing a product in the woo admin, the "onclick" part is automatically removed from the short description when saving the modifications, even when the short description hasn't been modified.

I am not familiar with WooCommerce nor with shortcodes nor with the best ways to use JavaScript in this environment.

So my question : is there a way to prevent the "onclick" part to be removed when saving changes, and if not, what is the best way to achieve the insertion of JavaScript inside a product short description, and keep it untouched when a product is edited ?

Thanks in advance.

Upvotes: 0

Views: 285

Answers (1)

Firefog
Firefog

Reputation: 3174

Here is an example of how you can do this. You should add your script below code and add it in your theme functions.php. Your question is kinda unclear please share your code so we can understand your code and write the answer correctly.

function firefog_add_script_short_descr(){
  ?>
  <script>
    jQuery(document).ready(function($) {
     
     <!-- Your jquery code goes here-->
     
    })
  </script>
<?php  
}
add_filter('woocommerce_before_add_to_cart_form','firefog_add_script_short_descr');

Upvotes: 2

Related Questions