'remove attribute class and id and style from posts wordpress

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



Solution 1:[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();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1