WordPress已删除标签/分类添加301重定向插件失效求助
WordPress删除标签/分类自动添加301重定向到首页的插件修复方案
我写了一款WordPress插件,希望在wp-admin删除标签或分类时,自动在.htaccess文件中添加301重定向规则到首页。类似的文章删除重定向代码能正常运行,但当前代码对标签和分类都无效。我不想把所有404页面都重定向到首页,避免引发404攻击,试了其他代码也没解决问题。原代码如下:
<?php /* Plugin Name: Redirect Deleted Tags Description: Redirects deleted tags to the homepage with a 301 redirect. Version: 1.0 Author: [Your Name] */ function redirect_deleted_term_to_home($term_id, $tt_id, $taxonomy) { // Check if the term is a tag or category if ($taxonomy === 'post_tag' || $taxonomy === 'category') { // Get the term object $term = get_term($term_id, $taxonomy); // Check if the term count is 0 (indicating it's been deleted) if ($term && $term->count === 0) { // Get the home URL $home_url = home_url('/'); // Generate the Redirect 301 rule $redirect_rule = "Redirect 301 " . get_term_link($term) . " $home_url "; // Path to the .htaccess file $htaccess_file = ABSPATH . '.htaccess'; // Check if .htaccess file exists and is writable if (file_exists($htaccess_file) && is_writable($htaccess_file)) { // Get the content of .htaccess file $htaccess_content = file_get_contents($htaccess_file); // Check if the redirect rule already exists if (strpos($htaccess_content, $redirect_rule) === false) { // Append the redirect rule to .htaccess file_put_contents($htaccess_file, PHP_EOL . "# Redirect deleted term to home " . $redirect_rule, FILE_APPEND | LOCK_EX); } } } } } add_action('wp_insert_term', 'redirect_deleted_term_to_home', 10, 3); ?>
问题分析
原代码的核心缺陷:
- 钩子使用错误:
wp_insert_term是添加术语时触发的钩子,删除标签/分类时根本不会执行该函数,必须替换为delete_term(删除术语时触发的钩子)。 - 术语对象获取不可靠:删除术语后调用
get_term大概率无法获取有效数据,而delete_term钩子会直接传入被删除的术语实例,无需额外查询。 - 重定向路径不通用:
get_term_link返回完整URL,Apache的Redirect指令用相对路径更稳妥,避免域名变动导致规则失效。
修正后的代码
<?php /* Plugin Name: Redirect Deleted Terms to Home Description: 自动给删除的标签/分类添加301重定向到首页 Version: 1.1 Author: [Your Name] */ function redirect_deleted_term_to_home($term_id, $tt_id, $taxonomy, $deleted_term) { // 仅处理标签和分类 if (!in_array($taxonomy, ['post_tag', 'category'])) { return; } // 获取被删除术语的链接,处理可能的错误 $term_url = get_term_link($deleted_term); if (is_wp_error($term_url)) { return; } // 解析URL得到站点根目录相对路径 $home_url = home_url('/'); $relative_path = str_replace($home_url, '/', $term_url); $relative_path = trailingslashit($relative_path); // 生成重定向规则 $redirect_rule = "Redirect 301 {$relative_path} {$home_url}\n"; $htaccess_file = ABSPATH . '.htaccess'; // 检查.htaccess权限 if (!file_exists($htaccess_file) || !is_writable($htaccess_file)) { return; } // 避免重复添加规则 $htaccess_content = file_get_contents($htaccess_file); if (strpos($htaccess_content, $relative_path) !== false) { return; } // 将规则写入.htaccess $comment = "# 重定向已删除的{$taxonomy}到首页\n"; file_put_contents($htaccess_file, PHP_EOL . $comment . $redirect_rule, FILE_APPEND | LOCK_EX); } // 绑定delete_term钩子,指定接收4个参数 add_action('delete_term', 'redirect_deleted_term_to_home', 10, 4); ?>
关键修改说明
- 改用
delete_term钩子并接收4个参数,直接使用钩子传入的$deleted_term获取被删除术语的信息。 - 解析URL得到相对路径,让重定向规则不受域名变更影响。
- 增加错误判断逻辑,避免无效术语导致的报错。
- 通过术语相对路径判断规则是否已存在,避免因换行、空格差异导致重复添加。
内容的提问来源于stack exchange,提问作者IT Guy
相关产品推荐
相关产品推荐

