问题描述

如果您在 WordPress 中输入:),它会自动替换为:

有没有办法为:)使用不同的笑脸

最佳解决方案

覆盖:) 的表情符号到ĒŸÂ~ÂŽ

内容表情符号转换为:

add_filter( 'the_content', 'convert_smilies' );

其中 convert_smilies()功能的 this part 非常重要:

$content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );

如果我们偷看 translate_smiley(),那么我们找到 following

// Don't convert smilies that aren't images - they're probably emoji.
if ( ! in_array( $ext, $image_exts ) ) {
     return $img;
}

之前应用 smilies_src 滤镜。

所以这个过滤器在:)笑脸的情况下是不可用的。

我们的初始化为:

add_action( 'init', 'smilies_init', 5 );

smilies_init()的功能描述中,我们可以阅读 following

Plugins may override the default smiley list by setting the $wpsmiliestrans to an array, with the key the code the blogger types in and the value the image file.

这是全球 $wpsmiliestrans 阵列:

$wpsmiliestrans = array(
    ':mrgreen:' => 'mrgreen.png',
    ':neutral:' => "xf0x9fx98x90",
    ':twisted:' => "xf0x9fx98x88",
    ':arrow:' => "xe2x9exa1",
    ':shock:' => "xf0x9fx98xaf",
    ':smile:' => "xf0x9fx99x82",
    ':???:' => "xf0x9fx98x95",
    ':cool:' => "xf0x9fx98x8e",
    ':evil:' => "xf0x9fx91xbf",
    ':grin:' => "xf0x9fx98x80",
    ':idea:' => "xf0x9fx92xa1",
    ':oops:' => "xf0x9fx98xb3",
    ':razz:' => "xf0x9fx98x9b",
    ':roll:' => 'rolleyes.png',
    ':wink:' => "xf0x9fx98x89",
    ':cry:' => "xf0x9fx98xa5",
    ':eek:' => "xf0x9fx98xae",
    ':lol:' => "xf0x9fx98x86",
    ':mad:' => "xf0x9fx98xa1",
    ':sad:' => "xf0x9fx99x81",
    '8-)' => "xf0x9fx98x8e",
    '8-O' => "xf0x9fx98xaf",
    ':-(' => "xf0x9fx99x81",
    ':-)' => "xf0x9fx99x82",
    ':-?' => "xf0x9fx98x95",
    ':-D' => "xf0x9fx98x80",
    ':-P' => "xf0x9fx98x9b",
    ':-o' => "xf0x9fx98xae",
    ':-x' => "xf0x9fx98xa1",
    ':-|' => "xf0x9fx98x90",
    ';-)' => "xf0x9fx98x89",
    // This one transformation breaks regular text with frequency.
    //     '8)' => "xf0x9fx98x8e",
    '8O' => "xf0x9fx98xaf",
    ':(' => "xf0x9fx99x81",
    ':)' => "xf0x9fx99x82",
    ':?' => "xf0x9fx98x95",
    ':D' => "xf0x9fx98x80",
    ':P' => "xf0x9fx98x9b",
    ':o' => "xf0x9fx98xae",
    ':x' => "xf0x9fx98xa1",
    ':|' => "xf0x9fx98x90",
    ';)' => "xf0x9fx98x89",
    ':!:' => "xe2x9dx97",
    ':?:' => "xe2x9dx93",
);

或更好的 ksorted 显示:

Array
(
    [;-)] => 😉
    [;)] => 😉
    [:|] => 😐
    [:x] => 😡
    [:wink:] => 😉
    [:twisted:] => 😈
    [:smile:] => 🙂
    [:shock:] => 😯
    [:sad:] => 🙁
    [:roll:] => rolleyes.png
    [:razz:] => 😛
    [:oops:] => 😳
    [:o] => 😮
    [:neutral:] => 😐
    [:mrgreen:] => mrgreen.png
    [:mad:] => 😡
    [:lol:] => 😆
    [:idea:] => 💡
    [:grin:] => 😀
    [:evil:] => 👿
    [:eek:] => 😮
    [:cry:] => 😥
    [:cool:] => 😎
    [:arrow:] => ➡
    [:P] => 😛
    [:D] => 😀
    [:???:] => 😕
    [:?:] => ❓
    [:?] => 😕
    [:-|] => 😐
    [:-x] => 😡
    [:-o] => 😮
    [:-P] => 😛
    [:-D] => 😀
    [:-?] => 😕
    [:-)] => 🙂
    [:-(] => 🙁
    [:)] => 🙂
    [:(] => 🙁
    [:!:] => ❗
    [8O] => 😯
    [8-O] => 😯
    [8-)] => 😎
)

所以如果我正确地理解了上述的核心评论,那么我们可以做以下事情:

/**
 * :) as the cool emoji
 */
add_action( 'init', function() use ( &$wpsmiliestrans )
{
    if( is_array( $wpsmiliestrans ) && get_option( 'use_smilies' ) )
        $wpsmiliestrans[':)'] = $wpsmiliestrans[':cool:'];

}, 6 );

但这只适用于预定义的笑脸键,可以让 $wp_smiliessearch 工作。

但我不喜欢这个建议的方法,修改全局数组!希望还有另一个更好!

演示插件 – ĒŸŽ…

我试图提出一个申请。我不知道这是否已经存在,但这里是:

<?php
/**
 * Plugin Name: Santa's Smile In December
 * Description: Change the emoji of :) to the Santa Claus emoji, but only in December
 * Plugin URI:  https://wordpress.stackexchange.com/a/218496/26350
 */
add_action( 'init', function() use ( &$wpsmiliestrans )
{
    // :) as Santa Claus
    if(
           is_array( $wpsmiliestrans )
        && get_option( 'use_smilies' )
        && 12 == current_time( 'n' )
    )
        $wpsmiliestrans[':)'] = "xF0x9Fx8Ex85";

}, 6 );

感谢 Ismael Miguel 为全球 comment,我相应地重写了片段。

关于新的 smilies_trans 滤波器,Pieter Goosen 的 newly created newly created 门票 #35905

更新 – WordPress 4.7+

新的过滤器将在 WordPress 4.7+中为 available,但其名称将为 smilies,不是 smilies_trans

我们上面的例子可以写成:

add_filter( 'smilies', function( $smilies )
{
    if( isset( $smilies[':cool:'] ) )
        $smilies[':)'] = $smilies[':cool:'];

    return $smilies;
} );

或明确地与:

add_filter( 'smilies', function( $smilies )
{
    $smilies[':)'] = "xf0x9fx98x8e";

    return $smilies;
} );

演示插件变成:

<?php
/**
 * Plugin Name: Santa's Smile In December
 * Description: Change the emoji of :) to the Santa Claus emoji, but only in December
 * Plugin URI:  https://wordpress.stackexchange.com/a/218496/26350
 */

add_filter( 'smilies', function( $smilies )
{
    // :) as Santa Claus
    if( get_option( 'use_smilies' ) && 12 == current_time( 'n' ) )
        $smilies[':)'] = "xF0x9Fx8Ex85";

    return $smilies;
} );

我们不需要再混淆全球 $wpsmiliestrans 阵列了!

次佳解决方案

根据 WordPress Codex on using smilies

Upload the images you want with the same name to your server (say in wp-content/images/smilies) and put this in your theme’s function.php:

add_filter( 'smilies_src', 'my_custom_smilies_src', 10, 3 );    function my_custom_smilies_src($img_src, $img, $siteurl){        return $siteurl.'/wp-content/images/smilies/'.$img;    }

That will replace http://example.com/wp-includes/images/smilies/icon_question.gif with http://example.com/wp-content/images/smilies/icon_question.gif

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。