从帖子中删除the_content的所有图像?

发布于 2025-02-11 21:45:36 字数 196 浏览 2 评论 0原文

您好,我想帮助创建一个插件,以自动删除帖子中的所有图像。

可能的解决方案将此代码添加到您的主题的index.php文件:

?php> 
echo preg_replace('/<img[^>]+./','',get_the_content()); 
?>

将此代码转换为插件?

Hello I want help to create a plugin to automatically remove all the images in the postings.

Possible solution add this code to your theme’s index.php file:

?php> 
echo preg_replace('/<img[^>]+./','',get_the_content()); 
?>

Convert this code into a plugin??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2025-02-18 21:45:36

您可以为此purporse使用wp_kses。使用WP_KSES_ALLOD_HTML filter添加img元素的过滤器。

在您的functions.php中。

function theme_slug_kses_allowed_html($tags, $context) {
    switch($context) {
        case 'no-images': 
            $tags = wp_kses_allowed_html('post');
            unset( $tags['img'] );
            return $tags;
        default: 
            return $tags;
    }
}
add_filter( 'wp_kses_allowed_html', 'theme_slug_kses_allowed_html', 10, 2);

然后在index.php中。

echo wp_kses( get_the_content(), 'no-images' );

You can use the wp_kses for this purporse. Add the filter for the img element using the wp_kses_allowed_html filter.

In your functions.php.

function theme_slug_kses_allowed_html($tags, $context) {
    switch($context) {
        case 'no-images': 
            $tags = wp_kses_allowed_html('post');
            unset( $tags['img'] );
            return $tags;
        default: 
            return $tags;
    }
}
add_filter( 'wp_kses_allowed_html', 'theme_slug_kses_allowed_html', 10, 2);

Then in index.php.

echo wp_kses( get_the_content(), 'no-images' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文