WordPress

WordPress网站后台的文章列表增加删除文章及图片的链接按钮

2024年11月28日 · 本文共2460个字 · 预计阅读9分钟67次已读
目录
文章目录隐藏
  1. 前言
  2. 使用方法

前言

  WordPress后台删除文章通常是先把文章移至回收站,再前往回收站永久删除文章或者清空回收站,操作不方便。

WordPress网站后台的文章列表增加删除文章及图片的链接按钮

  可通过模板函数文件(functions.php),在文章列表中添加一个删除、删除(含图片)的链接按钮,方便快速删除文章或删除文章并删除图片。

WordPress网站后台的文章列表增加删除文章及图片的链接按钮

使用方法

复制以下代码,粘贴到当前主题的模板函数文件中(functions.php)并更新文件。

代码

// 在文章行动作中添加自定义操作按钮
add_filter('post_row_actions', 'custom_post_row_actions', 10, 2);
function custom_post_row_actions($actions, $post) {
    // 添加仅删除文章的链接
    $actions['delete_without_images'] = '<a href="' . wp_nonce_url(admin_url('admin.php?action=delete_post_without_images&post=' . $post->ID), 'delete-post_without_images_' . $post->ID) . '" onclick="return confirm(\'确定要删除这篇文章吗?\')" style="color: blue;">删除</a>';//方便区分删除链接按钮,增加了蓝色醒目提示
    // 添加删除文章及图片的链接
    $actions['delete_with_images'] = '<a href="' . wp_nonce_url(admin_url('admin.php?action=delete_post_with_images&post=' . $post->ID), 'delete-post_with_images_' . $post->ID) . '" onclick="return confirm(\'确定要删除这篇文章及其图片吗?\')" style="color: red;">删除(含图片)</a>';//方便区分删除链接按钮,增加了红色醒目提示
    return $actions;
}
// 添加文章删除动作
add_action('admin_action_delete_post_with_images', 'delete_post_with_images');
add_action('admin_action_delete_post_without_images', 'delete_post_without_images');
// 删除文章及其图片的函数
function delete_post_with_images() {
    // 检查nonce
    check_admin_referer('delete-post_with_images_' . $_GET['post']);
    // 获取文章ID
    $post_id = intval($_GET['post']);
    // 删除文章及其图片
    delete_post_images($post_id);
    // 重定向到文章列表
    wp_redirect(admin_url('edit.php?post_type=post&deleted=true'));
    exit;
}
// 仅删除文章的函数
function delete_post_without_images() {
    // 检查nonce
    check_admin_referer('delete-post_without_images_' . $_GET['post']);
    // 获取文章ID
    $post_id = intval($_GET['post']);
    // 仅删除文章
    wp_delete_post($post_id, true);
    // 重定向到文章列表
    wp_redirect(admin_url('edit.php?post_type=post&deleted=true'));
    exit;
}
// 删除文章及其图片的函数
function delete_post_images($post_id) {
    // 获取文章内容
    $post_content = get_post_field('post_content', $post_id);
    // 使用正则表达式匹配所有图片
    preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $post_content, $matches);
    // 如果找到图片
    if (isset($matches[1]) && count($matches[1]) > 0) {
        foreach ($matches[1] as $image_url) {
            // 从完整的URL中提取文件名
            $file_path = parse_url($image_url, PHP_URL_PATH);
            $file_name = basename($file_path);
            // 构造完整的文件路径
            $upload_dir = wp_upload_dir();
            $file = $upload_dir['basedir'] . '/' . $file_name;
            // 如果文件存在,则删除
            if (file_exists($file)) {
                @unlink($file); // 使用@符号抑制错误信息
            }
            // 同时删除数据库中的图片记录
            $attachment_id = attachment_url_to_postid($image_url);
            if ($attachment_id) {
                wp_delete_attachment($attachment_id, true); // 尝试删除附件,true表示从数据库中删除
            }
        }
    }
    // 获取特色图片ID
    $thumbnail_id = get_post_meta($post_id, '_thumbnail_id', true);
    if ($thumbnail_id) {
        // 删除特色图片
        wp_delete_attachment($thumbnail_id, true);
    }
    // 删除文章
    wp_delete_post($post_id, true);
}
WordPress网站后台的文章列表增加删除文章及图片的链接按钮
0 条评论

Powered by atecplugins.com