You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress文章插件功能升级:在去链接基础上添加不良词语替换

Hey there! Let's break this down simply—you’re totally right that this is a quick tweak, no need to stress over the code. I’ll walk you through modifying your existing WordPress plugin to handle both link removal and bad word replacement.

Step 1: Find the Core Content Filter in Your Plugin

First, locate where your plugin hooks into WordPress’s content processing. It almost certainly uses the the_content filter. Your existing link-removal code probably looks something like this:

add_filter('the_content', 'remove_all_links_from_posts');
function remove_all_links_from_posts($content) {
    // Existing logic to strip links (common example using regex)
    $content = preg_replace('/<a\s[^>]*>(.*?)<\/a>/i', '$1', $content);
    return $content;
}
Step 2: Add Bad Word Replacement Logic

We’ll extend this same function to handle word swaps. Just add a replacement array and a simple string replace call right after the link-removal code. Here’s the updated version:

add_filter('the_content', 'remove_links_and_replace_bad_words');
function remove_links_and_replace_bad_words($content) {
    // Step 1: Keep your existing link-removal code
    $content = preg_replace('/<a\s[^>]*>(.*?)<\/a>/i', '$1', $content);

    // Step 2: Define your bad words and kid-friendly replacements
    $word_replacements = array(
        '不良词语1' => '阳光替代词1',
        '不良词语2' => '友好替代词2',
        'RudeSlang' => 'KindTerm'
        // Add as many pairs as you need here
    );

    // Replace the words (use str_ireplace for case-insensitive matches)
    $content = str_ireplace(array_keys($word_replacements), array_values($word_replacements), $content);

    // Return the cleaned-up content
    return $content;
}
Quick Customization Tips
  • Case Sensitivity: Use str_ireplace() (like above) to catch variations like "BadWord", "badword", and "BADWORD". If you need exact case matches only, switch to str_replace().
  • Special Characters: If your bad words include symbols (like hyphens or asterisks), you can add them directly to the array—no extra escaping needed for simple cases.
  • Testing: Always test this on a staging site first! Create a test post with links and your target bad words to confirm both features work as expected.

If your current plugin uses something like DOMDocument instead of regex to strip links, don’t panic. Just add the word replacement code after the link-removal logic finishes processing the $content variable. The structure stays the same—modify the content, then return it.

For example:

add_filter('the_content', 'remove_links_and_replace_bad_words');
function remove_links_and_replace_bad_words($content) {
    // Your existing DOM-based link removal code goes here
    // (after running this, $content has no links)

    // Add the word replacement section here
    $word_replacements = array(
        '不良词语1' => '阳光替代词1',
        // ...
    );
    $content = str_ireplace(array_keys($word_replacements), array_values($word_replacements), $content);

    return $content;
}

That’s all you need! You’re just extending the existing content-modification function instead of writing anything from scratch.

内容的提问来源于stack exchange,提问作者phpn00bcoder99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:43:00