Skip to content

Instantly share code, notes, and snippets.

@mr-fan
Forked from nefigah/wraptext.php
Created June 15, 2014 22:45
Show Gist options
  • Save mr-fan/68b3500f2f26722791ff to your computer and use it in GitHub Desktop.
Save mr-fan/68b3500f2f26722791ff to your computer and use it in GitHub Desktop.
<?php
// Let's assume the pages in question are named like Blah1, Blah2, etc.
foreach ($pages->find('name^=Blah') as $blahPage) {
// Let's assume the fields that need to contain HTML are called Html1, etc.
foreach ($blahPage->fields->find('name^=Html') as $field) {
// Let's assume these fields have their value in ShouldBeHtml
$fieldContent = $field->ShouldBeHtml;
$contentWrapped = '';
// Parse the field's content as HTML
$doc = new DOMDocument();
$doc->loadHTML($fieldContent);
$doc->normalizeDocument();
$doc->formatOutput = true;
// DOMDocument will wrap in html and body tag if it lacks these; I account for this
foreach ($doc->getElementsByTagName("body") as $body) {
$unwrapped = [];
foreach ($body->childNodes as $child) {
if ($child->nodeName === "#text") {
$unwrapped[] = $child;
}
}
foreach ($unwrapped as $child) {
$body->replaceChild($doc->createElement('p', $child->textContent), $child);
}
foreach ($body->childNodes as $child) {
$contentWrapped .= $doc->saveHTML($child);
}
}
// Save our changes to ProcessWire
$field->ShouldBeHtml = $contentWrapped;
$field->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment