skip to Main Content

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


  1. 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('/class="[^"]*elementor[^"]*"/', '', $content);
        $content = preg_replace('/[^"]*elementor[^"]*="/', '', $content);
        return $content;
    }
    
    Login or Signup to reply.
  2. Depending on your actual input snippets, better use DOM function instead, e.g.:

    <?php
    
    $snippet = <<<DATA
    <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>
    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;
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search