1

I'm getting unwanted

tags in my html code when I put it in an html widget block. I tried below methods but none of the seem to be working.

remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
remove_filter('widget_text_content', 'wpautop');
remove_filter('widget_custom_html_content', 'wpautop');

I entered this html code in the html widget box

<img href="test.jpg">
<span>text</span>
<script>alert("message");</script>

And I get the below code after the widgets are saved. (There are some "p" & "br" tags which isn't needed.)

<p><img href="test.jpg"><br>
<span>text</span><br>
<script>alert("message");</script></p>

(Please click and see the screenshot attached) Screenshot

Please help? Thanks!

Jaadu
  • 15
  • 6

1 Answers1

1

autops are auto generated html <p> tags around the wordpress content. If the "remove_filter" solution didn't work for you, then you could try something like this:

add_filter('widget_text_content', 'your_theme_removing_autop', 999);
add_filter('widget_custom_html_content', 'your_theme_removing_autop', 999);

function your_theme_removing_autop($content)
{
  $content = str_replace(['<p>', '</p>'], ['', ''], $content);
  return $content;
}

Tested and works on my end. Let me know if you could get it to work too!

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • 1
    Sure it would! But in this case there is only one `p` tag which is auto generated by wordpress. – Ruvee Sep 08 '21 at 08:36
  • What if there are multiple? How does this also remove the unwanted `
    ` (and possible keep others)?
    – Nico Haase Sep 08 '21 at 08:54
  • 1
    There are other workarounds for that scenario! Wordpress has other special functions to deal with those situations. For example, you could take a look at [this answer](https://stackoverflow.com/a/69029831/15040627) where i used "whitelisting" technique! – Ruvee Sep 08 '21 at 08:57