-
-
Save mr-fan/68b3500f2f26722791ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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