How To Automatically Close HTML Tags in the WYSIWYG Editor

Snippets by IsItWP

Are you looking for a way to automatically close HTML tags in WYSIWYG editor? While there’s probably a plugin for this, we have created a quick code snippet that you can use to automatically close HTML tags in the WYSIWYG editor in WordPress.


All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function clean_bad_content($bPrint = false) {
    global $post;
    $szPostContent  = $post->post_content;
    $szRemoveFilter = array("~<p[^>]*>\s?</p>~", "~<a[^>]*>\s?</a>~", "~<font[^>]*>~", "~<\/font>~", "~style\=\"[^\"]*\"~", "~<span[^>]*>\s?</span>~");
    $szPostContent  = preg_replace($szRemoveFilter, '', $szPostContent);
    $szPostContent  = apply_filters('the_content', $szPostContent);
 if ($bPrint == false) return $szPostContent; 
    else echo $szPostContent;

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 27 Best WordPress Plugins that You MUST Use and how to start a blog.

Comments   Leave a Reply

  1. Tweets that mention Wordpress Automatically close HTML in WYSIWYG – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com February 7, 2011 at 10:44 am

    […] This post was mentioned on Twitter by Enk. and Devox Solutions, WPSNIPP. WPSNIPP said: #wordpress Automatically close HTML in WYSIWYG http://bit.ly/hgYkxX #blog please RT 🙂 […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!