Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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... '-)

share|improve this answer
    
I've downloaded the trial and looking forward to checking it out. Thanks again for the nudge ;) –  Scott B Jan 30 '11 at 22:11
    
thanks for the help. I'm getting the same error with $postarr['post_content'] that I got before. –  Scott B Jan 30 '11 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... :) –  MikeSchinkel Jan 31 '11 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: –  MikeSchinkel Jan 31 '11 at 1:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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