How to configure WordPress to be able to use <script> tag inside posts?

I’m looking for a solution that would allow me to write tags inside posts and be sure that the visual editor or wordpress will not alter them.

The same problem can apply for other specific HTML code that I may want to use.

Read More

Disabling the visual editor is not an option, because in will render most edit operation too hard to use.

Related posts

Leave a Reply

2 comments

  1. Add the following to your theme functions.php:

    function fb_change_mce_options($initArray) {
        $ext = 'script[charset|defer|language|src|type]';
    
        if ( isset( $initArray['extended_valid_elements'] ) ) {
            $initArray['extended_valid_elements'] .= ',' . $ext;
        } else {
            $initArray['extended_valid_elements'] = $ext;
        }
    
        return $initArray;
    }
    add_filter('tiny_mce_before_init', 'fb_change_mce_options');
    
  2. I tried the accepted answer above and it didn’t work for me on WordPress 3.5.1

    I looked inside wp-includes/kses.php and it said to use the ‘wp_kses_allowed_html’ filter.
    This ended up working for me. You can replace the height and width attributes with any other script tag attributes such as type, language, etc.. depending on the tag you are trying to insert.

    function allow_script_tags( $allowedposttags ){
      $allowedposttags['script'] = array(
          'src' => true,
          'height' => true,
          'width' => true,
        );
      return $allowedposttags;
    }
    
    add_filter('wp_kses_allowed_html','allow_script_tags', 1);