0

WordPress (i.e. TinyMCE) automatically strips the <style> tag from articles in newer version. This means, you can only rely on inline CSS which makes the code ugly for more complex styling options and is also limited.

In my case, I want to include :hover functionality, but :hover does not work with inline CSS. Using the <style> tag in the body does not work either though.

I've tried this fix/hack, but it doesn't work with newer versions of WordPress.

Any suggestions how to change this?

(An alternative would be to get WordPress to accept the inclusion of external CSS files for individual articles.)

  • Apparently you still CAN use the <style> tag as long as you never switch to WYSIWYG mode. Pretty bothersome, but better than nothing. – R.G. Dec 9 '15 at 15:59
0

The <style> tag needs to be added to tinyMCE's extended_valid_elements array to prevent on that tag from being stomped:

function wpse211235_add_tiny_mce_before_init( $options ) {

    if ( ! isset( $options['extended_valid_elements'] ) ) {
            $options['extended_valid_elements'] = 'style';
    } else {
            $options['extended_valid_elements'] .= ',style';
    }

    return $options;
}
add_filter( 'tiny_mce_before_init', 'wpse211235_add_tiny_mce_before_init' );
  • 1
    and the reason it is not permitted to begin with, it is that css is not secure. I am not sure that kses will not strip it on post save for non admins (do non admins get the text editor at all? don't remember). maybe limiting this code to super admins is the best way to avoid confusion – Mark Kaplun Sep 21 '16 at 6:04
  • Good point, Mark. It looks like wp_kses does allow <style> tags, but only a limited number of attributes. Here's a link on customizing wp_kses()Why is wp_kses not keeping style attributes as expected?. – Dave Romsey Sep 21 '16 at 6:11
  • hmm, that question is about the style attribute, this one is about the style element. Too lazy right now to check myself if there is a difference ;) – Mark Kaplun Sep 21 '16 at 6:16
  • Yep, you're right! The question I referenced is about attributes. It's still possible to add the <style> tag to wp_kses's allowed tags (by default style is not one of them, but as you've pointed out, it's probably not a good idea anyway. – Dave Romsey Sep 21 '16 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.