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 class="elementor-widget-container"></div>
$re = "/ ?<div[^<]*?class=["'][^"']*\belementor-widget-container\b[^"']*["'][^<]*?>| ?<\/div>/s";
$content = preg_replace($re, "", $content);
// remove <div class="elementor-element"></div>
$re = "/ ?<div[^<]*?class=["'][^"']*\belementor-element\b[^"']*["'][^<]*?>| ?<\/div>/s";
$content = preg_replace($re, "", $content);
// remove <div class="elementor-container"></div>
$re = "/ ?<div[^<]*?class=["'][^"']*\belementor-container\b[^"']*["'][^<]*?>| ?<\/div>/s";
$content = preg_replace($re, "", $content);
// remove <div class="elementor-section-wrap"></div>
$re = "/ ?<div[^<]*?class=["'][^"']*\belementor-section-wrap\b[^"']*["'][^<]*?>| ?<\/div>/s";
$content = preg_replace($re, "", $content);
// remove <section class="elementor-section"></div>
$re = "/ ?<section[^<]*?class=["'][^"']*\belementor-section\b[^"']*["'][^<]*?>| ?<\/section>/s";
$content = preg_replace($re, "", $content);
// remove <div class="elementor"></div>
$re = "/ ?<div[^<]*?class=["'][^"']*\belementor\b[^"']*["'][^<]*?>| ?<\/div/s";
$content = preg_replace($re, "", $content);
return $content;
}
the html content before removing looks like this
<div class="page-content">
<div data-elementor-type="wp-page" data-elementor-id="257" data-post-id="257" data-obj-id="257" class="elementor elementor-257 dce-elementor-post-257" data-elementor-settings="[]">
<div class="elementor-section-wrap">
<section class="elementor-section elementor-top-section elementor-element elementor-element-2e18164 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="2e18164" data-element_type="section">
<div class="elementor-container elementor-column-gap-default">
<div class="elementor-element elementor-element-1aa904f elementor-widget elementor-widget-dce-rawphp" data-id="1aa904f" data-element_type="widget" data-widget_type="dce-rawphp.default">
<div class="elementor-widget-container">
<!-- Dynamic PHP Raw -->dies ist der normale Inhalt
</div>
</div>
</div>
</section>
</div>
</div>
<div class="post-tags">
</div>
</div>
the html content after my code looks like this
<div class="page-content">
<!-- Dynamic PHP Raw -->dies ist der normale Inhalt
<div class="post-tags">
</div>
</div>
thank you
2
Answers
This removes all the classes and properties of the tags in your HTML code that contain elementor:
Depending on your actual input snippets, better use DOM function instead, e.g.: