skip to Main Content

i want remove attribute class and id and style from posts wordpress

i see in all posts class and id and same of style attribute

<h2 id="gydde" style="color:#fff" class="iyjf"><span id="tttu" class="jfrrg"><b class="gr5" id="krttn" style="color:#fff">text</b></span></h2>

i want after save post clean code in posts saved in database like this

<h2><span><b>text</b></span></h2>

any way to do check and filter posts after publish and update and remove id and class

i see for php

    <?php
function stripUnwantedTagsAndAttrs($html_str){
  $xml = new DOMDocument();
//Suppress warnings: proper error handling is beyond scope of example
  libxml_use_internal_errors(true);
//List the tags you want to allow here, NOTE you MUST allow html and body otherwise entire string will be cleared
  $allowed_tags = array("html", "body", "b", "br", "em", "hr", "i", "li", "ol", "p", "s", "span", "table", "tr", "td", "u", "ul");
//List the attributes you want to allow here
  $allowed_attrs = array ("class", "id", "style");
  if (!strlen($html_str)){return false;}
  if ($xml->loadHTML($html_str, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)){
    foreach ($xml->getElementsByTagName("*") as $tag){
      if (!in_array($tag->tagName, $allowed_tags)){
        $tag->parentNode->removeChild($tag);
      }else{
        foreach ($tag->attributes as $attr){
          if (!in_array($attr->nodeName, $allowed_attrs)){
            $tag->removeAttribute($attr->nodeName);
          }
        }
      }
    }
  }
  return $xml->saveHTML();
}
?>

i dont know how to it for wordpress with remove attributes class id and style

2

Answers


  1. The PHP method you posted in the question can be implemented with WordPress filter:

    https://developer.wordpress.org/reference/hooks/field_no_prefix_save_pre/

    So the code should look something like:

    <?php
    
    add_filter( 'content_save_pre' , 'my_sanitize_content' , 10, 1);
    
    function my_sanitize_content( $content ) {
        $xml = new DOMDocument();
        //Suppress warnings: proper error handling is beyond scope of example
        libxml_use_internal_errors(true);
        //List the tags you want to allow here, NOTE you MUST allow html and body otherwise entire string will be cleared
        $allowed_tags = array("html", "body", "b", "br", "em", "hr", "i", "li", "ol", "p", "s", "span", "table", "tr", "td", "u", "ul");
        //List the attributes you want to allow here for example ["class", "id", "style"]
        $allowed_attrs = [];
        if (!strlen($html_str)){return false;}
        if ($xml->loadHTML($html_str, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)){
        foreach ($xml->getElementsByTagName("*") as $tag){
          if (!in_array($tag->tagName, $allowed_tags)){
            $tag->parentNode->removeChild($tag);
          }else{
            foreach ($tag->attributes as $attr){
              if (!in_array($attr->nodeName, $allowed_attrs)){
                $tag->removeAttribute($attr->nodeName);
              }
            }
          }
        }
      }
      return $xml->saveHTML();
    }
    
    Login or Signup to reply.
  2. Alternative to avoid quotes and encodes problems:

    add_filter('content_save_pre', 'sanitize_attrs_content', 10, 1);
    function sanitize_attrs_content($content)
    {
        if (empty($content)) {
            return false;
        }
    
        $tags = array('b', 'br', 'em', 'hr', 'i', 'li', 'ol', 'p', 's', 'span', 'table', 'tr', 'td', 'u', 'ul', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'blockquote');
        foreach ($tags as $tag) {
            $content = preg_replace('/<' . $tag . 'b[^>]*>/i', '<' . $tag . '>', $content);
            $content = preg_replace('/</' . $tag . '>/i', '</' . $tag . '>', $content);
            $content = preg_replace('/<' . $tag . 'b[^>]*>/i', '<' . $tag . '>', $content);
            $content = preg_replace('/(<' . $tag . 'b[^>]*)(id|class|style)="[^"]*"/i', '$1', $content);
        }
    
        return $content;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search