Avoid WordPress TinyMCE editor to automatically filter onClick/JS code

useWordPress website, can achieve many functions.

Sometimes we need to control the effect of object click through the onClick event.

  • For example, when embedding Google Maps, in a mask, scrolling the screen will not cause Google Maps to zoom.
  • However, when using the code added by the onClick event, when WordPress switches to "Visual Editor", the code will be cleared, causing the page to become invalid, very troublesome...

Avoid automatic deletion of code by the WordPress visual editor

To completely avoid "WordPress stripping" that code, you can add the following to your functions.php file:

//避免TINYMCE编辑器自动过滤onclick、JS、注释代码
function mod_mce($initArray) {
    $initArray['verify_html'] = false;
    return $initArray;
}
add_filter('tiny_mce_before_init', 'mod_mce');

The above method is very simple, just follow the operation, you can solve the problem.

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top