<p>The <em>Purify</em> setting for the Rich Text field will run the data through <a href="http://htmlpurifier.org/" rel="nofollow">HTMLPurifier</a>, which will do its best to try and remove any malicious code from the data.</p>

<p>I think what you're seeing is Redactor itself cleaning the rogue HTML elements before submitting to POST/Craft.</p>

<p>I think it's probably going to be easier trying to clean your HTML with some custom logic/regex before saving rather than trying to figure out how to spin up Redactor to do the cleaning.</p>

CC BY-SA 3.0
2015-05-02T19:01:25.313Z
2015-05-02T19:01:25.313Z
Brad Bell
<p>I'm writing a plugin to import posts from a WordPress blog. The source post formats are inconsistent, some coming with full HTML (<code>&lt;p&gt;</code> tags wrapping each paragraph, etc.), while others seem to just have tags added for formatting and emphasis—newlines but no paragraphs or <code>&lt;br&gt;</code>'s.</p>

<pre><code> $entry-&gt;setContentFromPost(array(
     'body' =&gt; $post['content'],
 ));

 $success = craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>So without any adjustment, many of the imported posts look crappy on the front end when they're just dropped into a template with <code>{{ entry.body }}</code> (Rich Text). The missing paragraph tags look less than stellar, and they're semantically uncool as well.</p>

<p>Redactor handles this just fine, so viewing and saving the imported entry in Craft is a breeze. It's not ideal, however, to go back and manually re-save each entry. <strong>Is there a way to re-save a cleaned Rich Text field via Craft's API, or do I need to standardize post HTML prior to my Craft import process?</strong></p>

CC BY-SA 3.0
2015-05-02T15:11:09.757Z
Unless I'm missing something, wouldn't `craft()->entries->saveEntry()` work?
Brad Bell
CC BY-SA 3.0
2015-05-02T15:17:37.810Z
@BradBell That's what I'm using, setting content on a Rich Text field with _Purify_ on and _Clean Up_ off. Perhaps it's my field settings? (Added minimally useful snippet to post.)
Matt Stein
CC BY-SA 3.0
2015-05-02T17:42:26.267Z
*Purify* is just going to run it through [HTMLPurifier](http://htmlpurifier.org/), which will do it's best to remove what it thinks is malicious code.
Brad Bell
CC BY-SA 3.0
2015-05-02T17:48:22.567Z
I think maybe what you're seeing is Redactor itself cleaning the HTML?
Brad Bell
CC BY-SA 3.0
2015-05-02T17:53:08.053Z
@BradBell That's what I suspected, which implies manually re-saving Redactor's work or cleaning HTML before `craft()->entries->saveEntry()`. From your comments here, it's obvious that a) I've asked another crappy question and b) there's no magic _Redactorize_ method I've failed to find and appreciate.
Matt Stein
CC BY-SA 3.0
2015-05-02T18:12:50.947Z
It's probably going to be easier trying to clean the HTML with some custom logic/regex before saving rather than trying to figure out how to spin up Redactor to do the cleaning, I'd guess.
Brad Bell
CC BY-SA 3.0
2015-05-02T18:14:54.083Z
@BradBell Then consider it confirmed by top people! If you answer to the same effect, I'll accept. Otherwise I should probably just delete this question.
Matt Stein
<plugin-development><entries><redactor>
Is there a way to programmatically clean/Redactorize HTML?
# Is there a way to programmatically clean/Redactorize HTML?

(Asked by: username_0 on 2015-05-02)

I'm writing a plugin to import posts from a WordPress blog. The source post formats are inconsistent, some coming with full HTML ( tags wrapping each paragraph, etc.), while others seem to just have tags added for formatting and emphasis—newlines but no paragraphs or 's.
 $entry->setContentFromPost(array(
     'body' => $post['content'],
 ));

 $success = craft()->entries->saveEntry($entry);

So without any adjustment, many of the imported posts look crappy on the front end when they're just dropped into a template with {{ entry.body }} (Rich Text). The missing paragraph tags look less than stellar, and they're semantically uncool as well.
Redactor handles this just fine, so viewing and saving the imported entry in Craft is a breeze. It's not ideal, however, to go back and manually re-save each entry. Is there a way to re-save a cleaned Rich Text field via Craft's API, or do I need to standardize post HTML prior to my Craft import process?

**Tags:** plugin-development, entries, redactor

> **Comments:**
> **username_1**: Unless I'm missing something, wouldn't `craft()->entries->saveEntry()` work?  
> **username_0**: @BradBell That's what I'm using, setting content on a Rich Text field with _Purify_ on and _Clean Up_ off. Perhaps it's my field settings? (Added minimally useful snippet to post.)  
> **username_1**: *Purify* is just going to run it through [HTMLPurifier](http://htmlpurifier.org/), which will do it's best to remove what it thinks is malicious code.  
> **username_1**: I think maybe what you're seeing is Redactor itself cleaning the HTML?  
> **username_0**: @BradBell That's what I suspected, which implies manually re-saving Redactor's work or cleaning HTML before `craft()->entries->saveEntry()`. From your comments here, it's obvious that a) I've asked another crappy question and b) there's no magic _Redactorize_ method I've failed to find and appreciate.  
> **username_1**: It's probably going to be easier trying to clean the HTML with some custom logic/regex before saving rather than trying to figure out how to spin up Redactor to do the cleaning, I'd guess.  
> **username_0**: @BradBell Then consider it confirmed by top people! If you answer to the same effect, I'll accept. Otherwise I should probably just delete this question.  

---

## 1 answers

### Answer by username_1 on 2015-05-02. Score: 1 | Accepted answer

The *Purify* setting for the Rich Text field will run the data through [HTMLPurifier](http://htmlpurifier.org/), which will do its best to try and remove any malicious code from the data.

I think what you're seeing is Redactor itself cleaning the rogue HTML elements before submitting to POST/Craft.

I think it's probably going to be easier trying to clean your HTML with some custom logic/regex before saving rather than trying to figure out how to spin up Redactor to do the cleaning.


---

