January 5, 2021

How to prevent WordPress from stripping HTML tags

If you use the classic editor when editing documents, you probably noticed that as soon as you switch the editing mode from Text to Visual, WordPress strips all the HTML tags (except the most basic ones) that you've so carefully crafted around your content. This is a hidden security feature of WordPress' TinyMCE; it is meant to prevent user-generated content (such as comments) from manipulating the layout of your pages or injecting malicious code.

However, if you don't require this layer of security, you don't need to install a cumbersome plugin. you can turn off the automatic stripping by adding a few lines of PHP code to your theme's functions.php

function allow_tags($val) {
   $opts = '*[*]';
   $val['valid_elements'] = $opts;
   $val['extended_valid_elements'] = $opts;
   return $val;
add_filter('tiny_mce_before_init', 'allow_tags');

Paste the above code in the functions.php of your theme or child theme, and you're free to style your content again.

Leave a Reply

Your email address will not be published. Required fields are marked *

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
Malcare WordPress Security