1

I'm writing a plugin to import posts from a WordPress blog. The source post formats are inconsistent, some coming with full HTML (<p> tags wrapping each paragraph, etc.), while others seem to just have tags added for formatting and emphasis—newlines but no paragraphs or <br>'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?

7
  • Unless I'm missing something, wouldn't craft()->entries->saveEntry() work?
    – Brad Bell
    May 2, 2015 at 15:11
  • @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
    May 2, 2015 at 15:17
  • Purify is just going to run it through HTMLPurifier, which will do it's best to remove what it thinks is malicious code.
    – Brad Bell
    May 2, 2015 at 17:42
  • I think maybe what you're seeing is Redactor itself cleaning the HTML?
    – Brad Bell
    May 2, 2015 at 17:48
  • @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
    May 2, 2015 at 17:53

1 Answer 1

1

The Purify setting for the Rich Text field will run the data through HTMLPurifier, 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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.