Posted By

alp on 06/06/11


Tagged

escape wordpress code comments


Versions (?)

Wordpress: Escape HTML Entities in Comments


 / Published in: PHP
 

URL: http://konstruktors.com/blog/wordpress/1850-automatically-escape-html-entities-of-code-fragments-in-comments/

  1. /**
  2.  * Escape HTML Entities in Comments
  3.  * tell your commentators to wrap their code snippets in <code> tags
  4.  * http://konstruktors.com/blog/wordpress/1850-automatically-escape-html-entities-of-code-fragments-in-comments/
  5.  *
  6.  **/
  7. function myfunction_encode_code_in_comment($source) {
  8. $encoded = preg_replace_callback('/<code>(.*?)<\/code>/ims',
  9. create_function('$matches', '$matches[1] = preg_replace(array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "", $matches[1]);
  10. return "<code>" . htmlentities($matches[1]) . "</"."code>";'), $source);
  11. if ($encoded)
  12. return $encoded;
  13. else
  14. return $source;
  15. }
  16. add_filter('pre_comment_content', 'myfunction_encode_code_in_comment');

Report this snippet  

You need to login to post a comment.