I try to remove all html tags that Elementor creates but leave the text inside.
It works, but I'm very sure the RegEx can be simplified so I need only one replace. My code looks like this
I think the easiest way would be to create an array with class names and all tags that include one of these class names will then be removed. But I'm really struggeling with RegEX
if( !is_admin() ) // not admin side
add_filter( 'the_content', 'remove_all_elementor_tags' );
function remove_all_elementor_tags( $content )
{
// remove <div ></div>
$re = "/ ?<div[^<]*?class=[\"'][^\"']*\\belementor-widget-container\\b[^\"']*[\"'][^<]*?>| ?<\\/div>/s";
$content = preg_replace($re, "", $content);
// remove <div ></div>
$re = "/ ?<div[^<]*?class=[\"'][^\"']*\\belementor-element\\b[^\"']*[\"'][^<]*?>| ?<\\/div>/s";
$content = preg_replace($re, "", $content);
// remove <div ></div>
$re = "/ ?<div[^<]*?class=[\"'][^\"']*\\belementor-container\\b[^\"']*[\"'][^<]*?>| ?<\\/div>/s";
$content = preg_replace($re, "", $content);
// remove <div ></div>
$re = "/ ?<div[^<]*?class=[\"'][^\"']*\\belementor-section-wrap\\b[^\"']*[\"'][^<]*?>| ?<\\/div>/s";
$content = preg_replace($re, "", $content);
// remove <section ></div>
$re = "/ ?<section[^<]*?class=[\"'][^\"']*\\belementor-section\\b[^\"']*[\"'][^<]*?>| ?<\\/section>/s";
$content = preg_replace($re, "", $content);
// remove <div ></div>
$re = "/ ?<div[^<]*?class=[\"'][^\"']*\\belementor\\b[^\"']*[\"'][^<]*?>| ?<\\/div/s";
$content = preg_replace($re, "", $content);
return $content;
}
the html content before removing looks like this
<div >
<div data-elementor-type="wp-page" data-elementor-id="257" data-post-id="257" data-obj-id="257" data-elementor-settings="[]">
<div >
<section data-id="2e18164" data-element_type="section">
<div >
<div data-id="1aa904f" data-element_type="widget" data-widget_type="dce-rawphp.default">
<div >
<!-- Dynamic PHP Raw -->dies ist der normale Inhalt
</div>
</div>
</div>
</section>
</div>
</div>
<div >
</div>
</div>
the html content after my code looks like this
<div >
<!-- Dynamic PHP Raw -->dies ist der normale Inhalt
<div >
</div>
</div>
thank you
CodePudding user response:
Depending on your actual input snippets, better use DOM function instead, e.g.:
<?php
$snippet = <<<DATA
<div >
<div data-elementor-type="wp-page" data-elementor-id="257" data-post-id="257" data-obj-id="257" data-elementor-settings="[]">
<div >
<section data-id="2e18164" data-element_type="section">
<div >
<div data-id="1aa904f" data-element_type="widget" data-widget_type="dce-rawphp.default">
<div >
<!-- Dynamic PHP Raw -->dies ist der normale Inhalt
</div>
</div>
</div>
</section>
</div>
</div>
<div >
</div>
</div>
DATA;
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($snippet);//, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOWARNING);
libxml_clear_errors();
$xpath = new DOMXPath($dom,);
$widgets = $xpath->query("//div[@class='elementor-widget-container']");
foreach ($widgets as $widget) {
echo $widget->nodeValue;
}
?>
CodePudding user response:
This removes all the classes and properties of the tags in your HTML code that contain elementor:
function remove_all_elementor_classes($content)
{
$content = preg_replace('/]*elementor[^"]*"/', '', $content);
$content = preg_replace('/[^"]*elementor[^"]*="/', '', $content);
return $content;
}