Когда я удаляю знак @ из переменных $ d, $ x DOMdocument, указанных ниже, я получаю сообщение об ошибке ...
Предупреждение: DOMDocument :: loadHTML () [domdocument.loadhtml]: Пустая строка, предоставляемая в качестве входных данных в C: \ xampplite \ htdocs \ mysite \ wp-content \ plugins \ myplugin \ index.php в строке 50
В переменной содержимого $ при запускефункция ниже.Хотя я могу отобразить содержимое и получить строку.Чего мне не хватает?
add_filter('wp_insert_post_data', 'decorate_keyword');
function decorate_keyword($postarray) {
global $post;
$keyword = getKeyword($post);
/*
Even though I can echo $content, I'm getting the error referenced above.
I have to explicitly set it to a string to overcome the error.
*/
$content = $postarray['post_content'];
//$content = "this is a test phrase";
$d = new DOMDocument();
$d->loadHTML($content);
$x = new DOMXpath($d);
$nodes = $x->query("//text()[contains(.,'$keyword') and not(ancestor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6)]");
if ($nodes && $nodes->length) {
$node = $nodes->item(0);
// Split just before the keyword
$keynode = $node->splitText(strpos($node->textContent, $keyword));
// Split after the keyword
$node->nextSibling->splitText(strlen($keyword));
// Replace keyword with <b>keyword</b>
$replacement = $d->createElement('b', $keynode->textContent);
$keynode->parentNode->replaceChild($replacement, $keynode);
}
$postarray['post_content'] = $d;
return $postarray;
}