求助:如何让WordPress自动去除URL中的Zero Width Spaces?
Zero-width spaces (ZWSP, Unicode U+200B) are incredibly frustrating because they’re invisible but can break URLs completely—especially when pasting from Outlook, which sometimes inserts them accidentally. Since this issue only pops up on some sites and with TinyMCE’s link tool, the fix needs to target both the editor experience and the saved content to be reliable.
Here are two straightforward, effective solutions:
1. Server-Side Filter (Reliable Safety Net)
This ensures no zero-width spaces make it into your database, regardless of how content is added (visual editor, text editor, or even programmatically). Add this code to your theme’s functions.php file or a custom plugin:
function strip_invisible_spaces($content) { // Remove zero-width spaces and related invisible Unicode characters $clean_content = preg_replace('/[\x{200B}\x{200C}\x{200D}\x{FEFF}]/u', '', $content); return $clean_content; } // Apply to post content, excerpts, and titles add_filter('content_save_pre', 'strip_invisible_spaces'); add_filter('excerpt_save_pre', 'strip_invisible_spaces'); add_filter('title_save_pre', 'strip_invisible_spaces');
This code hooks into WordPress’s content-saving process and strips out ZWSP along with similar invisible characters (like zero-width non-joiners and byte order marks) that can cause similar issues. It’s broad but safe—these characters are almost never intentional in post content or URLs.
If you only want to target URLs specifically (instead of the entire content), use this variation:
function strip_zwsp_from_urls($content) { // Clean ZWSP from any HTTP/HTTPS URLs in the content $content = preg_replace_callback('/https?:\/\/[^\s]+/u', function($matches) { return preg_replace('/[\x{200B}\x{200C}\x{200D}\x{FEFF}]/u', '', $matches[0]); }, $content); return $content; } add_filter('content_save_pre', 'strip_zwsp_from_urls');
2. Client-Side TinyMCE Fix (Real-Time Pasting Cleanup)
To prevent ZWSP from appearing in the editor at all (so users don’t accidentally save them before the server filter runs), add a TinyMCE plugin that cleans pasted content:
- First, add this code to your
functions.phpto register the plugin:
function add_tinymce_zwsp_stripper() { // Only load for users who can edit posts/pages and use rich editing if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) { return; } if ( get_user_option('rich_editing') !== 'true' ) { return; } // Register the TinyMCE plugin add_filter('mce_external_plugins', function($plugins) { $plugins['zwspstripper'] = admin_url('js/zwsp-stripper.js'); return $plugins; }); } add_action('admin_head', 'add_tinymce_zwsp_stripper');
- Create a new file named
zwsp-stripper.jsin your WordPresswp-admin/jsdirectory with this code:
tinymce.PluginManager.add('zwspstripper', function(editor) { // Listen for paste events and clean the content editor.on('paste', function(e) { e.content = e.content.replace(/[\u200B\u200C\u200D\uFEFF]/g, ''); }); });
This will automatically strip invisible spaces from any content pasted into the TinyMCE editor, so URLs look clean as soon as they’re added.
Why This Works for Your Scenario
WordPress 4.9.4’s default sanitization doesn’t account for zero-width spaces, which is why this issue only affects some sites (others may have custom sanitization already in place). Combining both fixes ensures you cover all bases: the server filter catches any missed cases, and the client-side fix improves the user experience.
内容的提问来源于stack exchange,提问作者Aaron A

