Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 3rd, 2010  |  syntax: PHP  |  size: 1.87 KB  |  views: 196  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // We need the <pre> tag to make code-in-backticks work
  2. function bporg_forums_allowed_tags( $component_allowedtags ) {
  3.         $component_allowedtags['pre'] = array();
  4.         return $component_allowedtags;
  5.        
  6. }
  7. add_filter( 'bp_forums_allowed_tags', 'bporg_forums_allowed_tags' );
  8. add_filter( 'bp_activity_allowed_tags', 'bporg_forums_allowed_tags' );
  9.  
  10. // Borrowed from bbPress
  11. function bporg_encodeit( $matches ) {
  12.         $text = trim($matches[2]);
  13.         $text = htmlspecialchars($text, ENT_QUOTES);
  14.         $text = str_replace(array("\r\n", "\r"), "\n", $text);
  15.         $text = preg_replace("|\n\n\n+|", "\n\n", $text);
  16.         $text = str_replace('&amp;amp;', '&amp;', $text);
  17.         $text = str_replace('&amp;lt;', '&lt;', $text);
  18.         $text = str_replace('&amp;gt;', '&gt;', $text);
  19.         $text = "<code>$text</code>";
  20.         if ( "`" != $matches[1] )
  21.                 $text = "<pre>$text</pre>";
  22.         return $text;
  23. }
  24.  
  25. // Borrowed from bbPress
  26. function bporg_decodeit( $matches ) {
  27.         $text = $matches[2];
  28.         $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES));
  29.         $text = strtr($text, $trans_table);
  30.         $text = str_replace('<br />', '<coded_br />', $text);
  31.         $text = str_replace('<p>', '<coded_p>', $text);
  32.         $text = str_replace('</p>', '</coded_p>', $text);
  33.         $text = str_replace(array('&#38;','&amp;'), '&', $text);
  34.         $text = str_replace('&#39;', "'", $text);
  35.         if ( '<pre><code>' == $matches[1] )
  36.                 $text = "\n$text\n";
  37.         return "`$text`";
  38. }
  39.  
  40. // Borrowed from bbPress. Makes code in backticks work, both in forum posts and in activity updates.
  41. function bporg_code_trick( $text ) {
  42.         $text = str_replace(array("\r\n", "\r"), "\n", $text);
  43.         $text = preg_replace_callback("|(`)(.*?)`|", 'bporg_encodeit', $text);
  44.         $text = preg_replace_callback("!(^|\n)`(.*?)`!s", 'bporg_encodeit', $text);
  45.         return $text;
  46. }
  47. add_filter( 'bp_get_the_topic_post_content', 'bporg_code_trick', 1 );
  48. add_filter( 'bp_get_activity_content_body', 'bporg_code_trick', 1 );
clone this paste RAW Paste Data