'filter html post before save/publish wordpress
i want filter html post code before publish or save or update to clean it from html att tags like id , class , style for html h1 , h2 , h3 , h4 , h5 , h6 , span , p , b , strong ,ol , etc so after save post output in editor from database well back clean without any style or id or class
so example old code before do filter
<h3 id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9." style="color:blue"><span id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9.">text example</span></h3>
text example
<h4><span id=".A7.D9.84.D8.BA.D8.AF.D8.A9_." style="color:red" class=".A9_.D8.A7.D9.84.D9.">text example</span></h4>
text example
<h2><span id="D9.82.D9.8A." class="AF.D8.">text example</span></h4>
text example
<h3><span id="AF.D8.B1.D9.82.D9.8A.D8.A9" class="82.D9.8A.D8.A9">text example</span></h3>
<ul id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9."><li id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9." style="color:black">test</li></ul>
want it to be in post after publish or save or update any post in database like this after filter
<h3><span>text example</span></h3>
text example
<h4><span>text example</span></h4>
text example
<h2><span>text example</span></h4>
text example
<h3><span>text example</span></h3>
<ul><li>test</li></ul>
hope help me
Solution 1:[1]
i hope this will work out
<?php
$data = '<h3 id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9." style="color:blue"><span id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9.">text example</span></h3>
text example
<h4><span id=".A7.D9.84.D8.BA.D8.AF.D8.A9_." style="color:red" class=".A9_.D8.A7.D9.84.D9.">text example</span></h4>
text example
<h2><span id="D9.82.D9.8A." class="AF.D8.">text example</span></h4>
text example
<h3><span id="AF.D8.B1.D9.82.D9.8A.D8.A9" class="82.D9.8A.D8.A9">text example</span></h3>
<ul id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9."><li id=".D8.AF.D8.A9_.D8.A7.D9.84.D8" class="A7.D9." style="color:black">test</li></ul>';
$html = preg_replace('/class=".*?"/', '', $data);
$html = preg_replace('/id=".*?"/', '', $html);
$html = preg_replace('/style=".*?"/', '', $html);
$html = preg_replace('/\s+/', '', $html);
echo $html;
?>
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 | atomankion |
