Back to blog
6 min read Tutorials

Clean Up Pasted Content in Bulk: Inline Styles, Empty Paragraphs, Word Markup

A guest author pastes articles from Word or Google Docs, and the HTML behind them is full of style="font-size:12.0pt", class="MsoNormal", <o:p> tags and empty paragraphs. The posts look slightly off on your site, and the theme’s fonts and colors don’t apply. With Import Export by RockStarLab PRO, one function cleans the markup of all those posts at once and keeps the text, links, bold and italic.

PRO: the Content Updater and custom functions. Use PRO 2.1.18 or newer: older versions changed &nbsp; in the saved code, so empty paragraphs with a non-breaking space stayed.

What the function removes

  • Inline styles: fonts, sizes, colors, margins and line heights.
  • Word markup: class="Mso…", <o:p> tags and Word’s conditional comments.
  • Google Docs markup: the <meta> tag and the bold wrapper around the whole text. Bold and italic set with styles become <strong> and <em>.
  • Old <font> tags, empty <span> tags and empty paragraphs, also with only &nbsp; or a line break.

Other classes, links, headings and images stay. Alignment and colors that were set with styles go too, so check posts where you wanted them.

The update changes the content of your posts. Back up the site first, or ask your AI agent to start the backup.

Step 1: Create the function

Open Import Export → Functions, click New Function, name it and paste the code:

// Clean HTML pasted from Word or Google Docs.
$html = (string) $value;
// Word's own tags: <o:p></o:p> and conditional comments.
$html = preg_replace('/<\/?o:p[^>]*>/i', '', $html);
$html = preg_replace('/<!--\[if[^\]]*\]>.*?<!\[endif\]-->/is', '', $html);
// Google Docs: the <meta> tag and the bold wrapper around the whole text.
$html = preg_replace('/<meta[^>]*>/i', '', $html);
$html = preg_replace('/<b[^>]*id="docs-internal-guid[^"]*"[^>]*>(.*)<\/b>/is', '$1', $html);
// Keep bold and italic that Google Docs sets with styles.
$html = preg_replace('/<span[^>]*font-weight:\s*(700|bold)[^>]*>(.*?)<\/span>/is', '<strong>$2</strong>', $html);
$html = preg_replace('/<span[^>]*font-style:\s*italic[^>]*>(.*?)<\/span>/is', '<em>$1</em>', $html);
// Inline styles, Mso classes and the old <font> tag.
$html = preg_replace('/\sstyle=("[^"]*"|\'[^\']*\')/i', '', $html);
$html = preg_replace('/\sclass=("Mso[^"]*"|\'Mso[^\']*\')/i', '', $html);
$html = preg_replace('/<\/?font[^>]*>/i', '', $html);
// <span> tags that are left without attributes.
$html = preg_replace('/<span>(.*?)<\/span>/is', '$1', $html);
// Empty paragraphs, also with only &nbsp; or <br>.
$html = preg_replace('/<p[^>]*>(\s|&nbsp;|&#160;|\xC2\xA0|<br\s*\/?>)*<\/p>/i', '', $html);
// Blank lines left behind.
$html = preg_replace("/\n{3,}/", "\n\n", $html);
return trim($html);

Test it with a piece of pasted HTML. Our Word paragraph lost its class, styles and <o:p>, and the empty paragraph was removed:

New Function: Clean pasted Word and Google Docs HTML, PHP code, test turning a Word paragraph into clean HTML
The function cleans a pasted Word paragraph.

Click Save Function.

Step 2: Choose the posts

Open Import Export → Content Updater and choose Blog Posts. All our pasted posts came from one guest author, so we added the filter Author Name equals Maria Lopez. It found her 3 posts:

Content Updater filter Author Name equals Maria Lopez, 3 items
The posts of the guest author.

Other ways to find pasted posts: Content contains MsoNormal for Word, or docs-internal-guid for Google Docs.

Step 3: Clean the content

In Select Fields to Update, click Content. In Field Transformations, click Assign Transformations, find the function, click Add and check the result with Test Pipeline:

Field Transformations for Content with the clean-up function and a Test Pipeline result
The function on the Content field, tested first.

Click Apply Transformations, go to the last step and click Start Update:

Content Updater: Update Complete, 3 processed, 3 updated, 0 errors
All 3 posts were cleaned.

The result

The post pasted from Word, before:

<p class="MsoNormal" style="margin-bottom:0cm;line-height:normal"><span style="font-size:12.0pt;font-family:&quot;Calibri&quot;,sans-serif">Cold brew needs time, not equipment. You need coarse coffee, cold water and a jar.<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom:0cm;line-height:normal"><span style="font-size:12.0pt">&nbsp;<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom:0cm"><b><span style="font-size:14.0pt;color:#1F3864">Step 1: Grind</span></b><span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt">Use 100 g of coffee, ground coarse like sea salt.<o:p></o:p></span></p>
<!--[if gte mso 9]><xml><w:WordDocument><w:View>Normal</w:View></w:WordDocument></xml><![endif]-->

After:

<p>Cold brew needs time, not equipment. You need coarse coffee, cold water and a jar.</p>

<p><b>Step 1: Grind</b></p>
<p>Use 100 g of coffee, ground coarse like sea salt.</p>

The post from Google Docs kept its bold “Tip:” as <strong>, and the post with <font> tags lost them and its two empty paragraphs.

Tips

  • Test on one post first. Add a filter on the post ID, run the update and look at the post before you clean all of them.
  • New posts. Run the update again after the next batch of guest posts. Clean posts don’t change.
  • Find posts with old markup. Export the posts with Content contains style= to see how many there are; see finding posts by their content.

With an AI agent

An AI agent connected to the site through the WordPress MCP Adapter (see connecting Claude Code to WordPress) can check that nothing was left behind:

Do any posts by Maria Lopez still have Word markup or inline styles?

In our run, the agent counted Maria Lopez’s posts with rsl-ie/export-count (3), then called rsl-ie/export-preview with the filters Author Name equals Maria Lopez and Content contains style=. The list was empty: no inline styles were left.

FAQ

Does it work with block editor posts?

Yes. It changes the HTML inside the blocks and keeps the block comments. Pasted content usually sits in paragraph or Classic blocks.

Yes. The function removes styles, Word and Google Docs markup and empty paragraphs, not tags such as headings, links or lists.

Can I undo it?

Restore the backup. WordPress revisions are not created by the Content Updater.

Is this in the free plugin?

No. The Content Updater and custom functions are PRO features.

Get the free plugin on WordPress.org.

Dmitriy
About author

Dmitriy

WordPress Developer with 15+ years of commercial experience. Elite Author on ThemeForest and creator of custom WordPress plugins. Passionate about clean code, performance optimization, and building useful tools for the WordPress community.

JOIN THE CONVERSATION

Comments 0

Have a question or a tip to share? Leave it below.

Leave a comment

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

We store your name, email and comment to show and moderate it; your email is never published. Spam protection is provided by Cloudflare Turnstile and profile pictures by Gravatar. See our Privacy Policy.


Try the plugin with no risks!

New users get 1 month with access to all premium features. Experience the most powerful WordPress data engine today.

Download Now
Get PRO for FREE