WordPress 替换文章的某些文字

有时候我们需要替换文章的某些文字为其它文字,比如给特定的文字加上链接,利用 SEO 和用户体验。

下边的代码即可实现这种功能:

/**
    *WordPress 替换文章的某些文字
*/
function netnote_content_str_replace($text){
    $replace_words = array(
        //添加要替换的文本,格式:'要替换的' => '替换到的内容'
        'netnote' => '<a href="http://www.endskin.com">EndSkin</a>',
        '爱博说' => '<a href="https://pdbn.top/">爱博说</a>',
        'WordPress' => '<a href="https://pdbn.top/category/nxdm/wp-notes/">WordPress</a>'
    );
    return str_replace( array_keys( $replace_words ), $replace_words, $text );
}

add_filter( 'the_content', 'netnote_content_str_replace' );
add_filter( 'the_excerpt', 'netnote_content_str_replace' );

来自http://www.endskin.com/content-str-replace/