0

The purpose of this function is to simply wrap the first occurrence of the keyword in bold tags. I'm getting an error on the line marked below.

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: error parsing attribute name in Entity, line: 4 in C:\xampplite\htdocs\testsite\wp-content\plugins\mylugin\test.php on line 40

Is this the correct way to pass the post_content object?

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 Answer 1

2

Hi @Scott B:

Here's a modified version of what you have that resolves the error (although the XPath does not match; I'm not an XPath guru so can't help with the proper XPath syntax):

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;
}

P.S. About that PhpStorm... '-)

4
  • I've downloaded the trial and looking forward to checking it out. Thanks again for the nudge ;)
    – Scott B
    Commented Jan 30, 2011 at 22:11
  • thanks for the help. I'm getting the same error with $postarr['post_content'] that I got before.
    – Scott B
    Commented Jan 30, 2011 at 22:16
  • @Scott B: My bad. Just when I think I can get away with not testing what I post... I did not fix the XPath to work correct because I'd have to learn XPath to do that, but I fixed your crashing issue from your original code. (Hint: PhpStorm+Zend Debugger... :) Commented Jan 31, 2011 at 1:05
  • @Scott B: BTW, definitely start with the Early Access Program version; it's much better than the shipping version and very stable: Commented Jan 31, 2011 at 1:19

Your Answer

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

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