简体   繁体   中英

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.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM