问:我尝试删除 Elementor 创建的所有 html 标签,但将文本留在里面。
它可以工作,但我很确定 RegEx 可以简化,所以我只需要一个替换。我的代码看起来像这样
我认为最简单的方法是创建一个带有类名的数组,然后删除包含这些类名之一的所有标签。但我真的很挣扎 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;
}
删除前的 html 内容如下所示
<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>
我的代码后的 html 内容如下所示
<div class="page-content">
<!-- Dynamic PHP Raw -->dies ist der normale Inhalt
<div class="post-tags">
</div>
</div>
谢谢
答:这将删除 HTML 代码中包含elementor的标签的所有类和属性:
function remove_all_elementor_classes($content)
{
$content = preg_replace('/class="[^"]*elementor[^"]*"/', '', $content);
$content = preg_replace('/[^"]*elementor[^"]*="/', '', $content);
return $content;
}