没有不值得去解决的问题,也没有不值得去学习的技术!

WordPress Tag Cleanup (Part 5): Resolve Leftover Chinese Tags in English With PHP/Go Scripts and Automate Tag Merging and URL Redirects

Screenshot 5: Verifying the 301 redirect

Preface

This article explains how to completely resolve the issue of residual Chinese tags under the English language and implement automated tag merging and URL redirection using a Go script.

Project URL: https://github.com/shuijingwan/tag-merge

Background

After completing the operations described in the previous blog posts, we found that some Chinese tags under the English language remained unprocessed:

Screenshot 1: Output of executing the command php fix-en-chinese-tags.php, 10 failed

Screenshot 1: Output of executing the command php fix-en-chinese-tags.php, 10 failed

These failures primarily occurred because the slug was already in use by another item, for example:

Plaintext
❌ Modification failed: The slug "console-command" is already in use by another item.
Screenshot 6: In the backend under the English language, sorting by name reveals that some Chinese tags remain unprocessed

Screenshot 6: In the backend under the English language, sorting by name reveals that some Chinese tags remain unprocessed

Problem Analysis

Screenshot 8: Analysis of failed cases

Screenshot 8: Analysis of failed cases

Using “控制台命令” as an example, the actual situation is as follows:

LanguageTag NameSlug
中文(中国)控制台命令控制台命令
中文(中国)Console/Commandconsole-command
English控制台命令控制台命令
EnglishConsole/Commandconsole-command

Root cause:

  1. Two different tags exist under the Chinese language: “控制台命令” and “Console/Command”.
  2. When attempting to change the English language tag “控制台命令” to “console command”, the slug console-command was already occupied by “Console/Command”.
  3. A mapping exists in the translation cache: "控制台命令": "console command"

Solution

Core idea: Change the tag name under the Chinese language to English, so that it matches the correct slug after translation.

Operation Steps

Step 1: Translate and modify the tag under the Chinese language

Screenshot 10: Editing the tag under the English language

Screenshot 10: Editing the tag under the English language

Change Console/Command console-command to console command console-command-en

Screenshot 11: Editing the tag under the Chinese language

Screenshot 11: Editing the tag under the Chinese language

Change Console/Command console-command to console command console-command

Note: The backend will automatically append the -en suffix to the English text, which is normal.

Screenshot 2: In the backend under the English language, searching for the tag 鼠标光标 yields no results
Screenshot 3: In the backend under the English language, searching for the tag mouse cursor returns results
Screenshot 4: In the backend under the 中文(中国) language, 鼠标光标 is associated with the tag mouse cursor under the English language

Screenshots 2-4: Verifying tag associations

After modification, the translation association between the Chinese “鼠标光标” and the English “mouse cursor” remains unchanged.

Step 2: Regenerate tag mappings

Screenshot 12: After re-executing the command go run main.go, output/tag_mapping_result.csv will contain 1206,控制台命令,2723,console command,缓存匹配成功

Screenshot 12: After re-executing the command go run main.go

Bash
go run main.go

A new mapping record will appear in the generated output/tag_mapping_result.csv:

Plaintext
1206,控制台命令,2723,console command,缓存匹配成功

Step 3: Execute tag merging

Screenshot 13: Executing the merge command

Screenshot 13: Executing the merge command

Bash
php merge-tags.php --all
php fix-en-chinese-tags.php

Output result:

Plaintext
📊 Successfully merged in this run: 10 mapping sets (merge-tags.php)
Processed 0 (fix-en-chinese-tags.php)
Failed 0 (fix-en-chinese-tags.php)

Step 4: Generate Nginx 301 rules

Screenshot 14: Viewing the generated rules

Screenshot 14: Viewing the generated rules
Bash
cd cmd/nginx-redirect/
go run main.go

Output:

Plaintext
📋 50 Nginx rules already exist
📝 Generated 20 new rules from merge_log.json, skipped 0 duplicate rules
✅ Successfully appended 20 new rules to ../../output/nginx_redirect.conf
Screenshot 5: Verifying the 301 redirect

Screenshot 5: Verifying the 301 redirect

Visiting /en/tag/鼠标光标/ will correctly redirect to /en/tag/mouse-cursor/

Step 5: Manually process remaining tags

Screenshot 15: Processing the remaining 3 tags

Screenshot 15: Processing the remaining 3 tags

For tags that do not exist in the translation cache, you need to manually modify them in the backend.

Summary of Results

Through this series of operations, we achieved:

  1. ✅ Translated all Chinese tags under the English language to English
  2. ✅ Automatically merged duplicate tags
  3. ✅ Generated 301 redirect rules to pass link equity
  4. ✅ Completely resolved the 404 error issue

Tool Description

The main tools used in this project:

ToolPurpose
main.goTranslation collision handling, generates tag mapping CSV
merge-tags.phpExecutes tag merging
fix-en-chinese-tags.phpFixes Chinese tags under the English language
nginx-redirect/main.goGenerates Nginx 301 redirect rules

For detailed usage instructions, please refer to the project README.

Conclusion

Using the method introduced in this article, we completely resolved the confusion between Chinese and English tags on the WordPress site. Most of the process is automated, greatly improving efficiency. I hope this series of articles has been helpful to you!

系列导航

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系