Advertisement
shaked

Remove wordpress's html p tags

Jan 19th, 2012
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. /**
  2. * DOM Handler
  3. */
  4. class Extended_DOMDocument extends DOMDocument {
  5. /**
  6. * @var DOMElement $pluginStauts
  7. */
  8. private $pluginStatus = false;
  9.  
  10. const CONTAINER = "our_plugins_class_wrapper";
  11. const UTF8_META = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  12.  
  13.  
  14. public function __construct($post){
  15. parent::__construct("1.0", "UTF-8");
  16. if (!@$this->loadHTML(self::UTF8_META.$post)){
  17. return ;
  18. }
  19. $this->isPluginInvolved();
  20. }
  21.  
  22. private function isPluginInvolved(){
  23. $this->pluginStatus = $this->getElementByClassName(self::CONTAINER);
  24. }
  25.  
  26. /**
  27. * Remove p tags
  28. * @return string
  29. */
  30. public function removeWpAutoP(){
  31. $content = $this->saveHTML();
  32. if (!$this->pluginStatus){
  33. return $content;
  34. }
  35. $html = self::DOMinnerHTML($this->pluginStatus);
  36. return str_replace($html,preg_replace('#<p>(.*?)<\/p>#','$1',$html),$content);
  37. }
  38.  
  39. /**
  40. * @see http://stackoverflow.com/questions/5404941/php-domdocument-outerhtml-for-element
  41. */
  42. public static function DOMinnerHTML($n, $outer=true) {
  43. $d = new DOMDocument('1.0');
  44. $b = $d->importNode($n->cloneNode(true),true);
  45. $d->appendChild($b); $h = $d->saveHTML();
  46. // remove outter tags
  47. if (!$outer) $h = substr($h,strpos($h,'>')+1,-(strlen($n->nodeName)+4));
  48. return $h;
  49. }
  50.  
  51. /**
  52. * Get all elements that have a tag of $tag and class of $className
  53. *
  54. * @param string $className The class name to search for
  55. * @param string $tag Tag of the items to search
  56. * @return array Array of DOMNode items that match
  57. */
  58. public function getElementsByClassName($className, $tag="*") {
  59. $nodes = array();
  60. $domNodeList = $this->getElementsByTagName($tag);
  61. for ($i = 0; $i < $domNodeList->length; $i++) {
  62. $item = $domNodeList->item($i)->attributes->getNamedItem('class');
  63. if ($item) {
  64. $classes = explode(" ", $item->nodeValue);
  65. for ($j = 0; $j < count($classes); $j++) {
  66. if ($classes[$j] == $className) {
  67. $nodes[] = $domNodeList->item($i);
  68. }
  69. }
  70. }
  71. }
  72. return $nodes;
  73. }
  74.  
  75. /**
  76. * Convenience method to return a single element by class name when we know there's only going to be one
  77. *
  78. * @param string $className The class name to search for
  79. * @param string $tag Tag of the items to search
  80. * @return array Array of DOMNode items that match
  81. */
  82. public function getElementByClassName($className, $tag="*") {
  83. $nodes = $this->getElementsByClassName($className, $tag);
  84. return count($nodes) == 1 ? $nodes[0] : $nodes;
  85. }
  86. }
  87.  
  88.  
  89. // setting this filter to priority 12
  90. add_filter('the_content' , array('removeWpAutoP'), 12);
  91. /**
  92. * remove auto wp p tags
  93. * @param string $content
  94. * @return string
  95. */
  96. function rm_wpautop($content) {
  97. $dom = new Extended_DOMDocument($content);
  98. $content = $dom->removeWpAutoP();
  99. return $content;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement