将post_content传递给函数时出错

时间:2011-01-30 作者:Scott B

此函数的目的是简单地将第一次出现的关键字包装为粗体标记。我在下面标记的行上发现一个错误。

警告:DOMDocument::loadHTML()[DOMDocument.loadHTML]:在C:\\xamplite\\htdocs\\testsite\\wp content\\plugins\\mylugin\\test中的第4行分析实体中的属性名称时出错。php在线40

这是传递post\\u内容对象的正确方法吗?

add_filter(\'wp_insert_post_data\', \'my_bold_keyword\' );
function my_bold_keyword($content){
  $myKeyword = "test 123";
  $d = new DOMDocument();
  $d->loadHTML($content[\'post_content\']); //ERROR OCCURS HERE
  $x = new DOMXpath($d);
  $Matches = $x->query("//text()[contains(.,$myKeyword) 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($Matches && $Matches->length > 0){
    $myText = $Matches->item(0);
    // need to wrap #myText in <b> wrapper
  }
  return $content;
}

1 个回复
最合适的回答,由SO网友:MikeSchinkel 整理而成

你好@Scott B:

下面是一个修改后的版本,可以解决这个错误(虽然XPath不匹配;我不是XPath专家,所以无法帮助您使用正确的XPath语法):

add_filter(\'wp_insert_post_data\', \'my_bold_keyword\', 10, 2 );
function my_bold_keyword( $data, $postarr ) {
  $myKeyword = "test 123";
  $d = new DOMDocument();
  $d->loadHTML($postarr[\'post_content\']); //ERROR NO LONGER OCCURS HERE
  $x = new DOMXpath($d);
  $Matches = $x->query("//text()[contains(.,$myKeyword) 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($Matches && $Matches->length > 0){
    $myText = $Matches->item(0);
    // need to wrap #myText in <b> wrapper
  }
  return $content;
}
附:关于PhpStorm…\'-)

结束

相关推荐