输入“ :)”时显示不同的笑脸


9

如果您输入:)WordPress,它将自动替换为:

在此处输入图片说明

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


嗨,krish,您能否提供更多有关您已经尝试过的内容的详细信息(以及为什么它不起作用)?
kraftner

Answers:


14

将:)的表情符号覆盖到😎

内容表情符号转换为:

add_filter( 'the_content', 'convert_smilies' );

其中,这部分的的convert_smilies()作用是非常重要的:

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

如果我们窥视一下,translate_smiley()就会发现以下内容

// 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()我们可以阅读以下内容

插件可以通过将设置$wpsmiliestrans 为一个数组来覆盖默认的笑脸列表,并使用博主输入的代码和图像文件的值作为键。

这是全局$wpsmiliestrans数组:

$wpsmiliestrans = array(
    ':mrgreen:' => 'mrgreen.png',
    ':neutral:' => "\xf0\x9f\x98\x90",
    ':twisted:' => "\xf0\x9f\x98\x88",
    ':arrow:' => "\xe2\x9e\xa1",
    ':shock:' => "\xf0\x9f\x98\xaf",
    ':smile:' => "\xf0\x9f\x99\x82",
    ':???:' => "\xf0\x9f\x98\x95",
    ':cool:' => "\xf0\x9f\x98\x8e",
    ':evil:' => "\xf0\x9f\x91\xbf",
    ':grin:' => "\xf0\x9f\x98\x80",
    ':idea:' => "\xf0\x9f\x92\xa1",
    ':oops:' => "\xf0\x9f\x98\xb3",
    ':razz:' => "\xf0\x9f\x98\x9b",
    ':roll:' => 'rolleyes.png',
    ':wink:' => "\xf0\x9f\x98\x89",
    ':cry:' => "\xf0\x9f\x98\xa5",
    ':eek:' => "\xf0\x9f\x98\xae",
    ':lol:' => "\xf0\x9f\x98\x86",
    ':mad:' => "\xf0\x9f\x98\xa1",
    ':sad:' => "\xf0\x9f\x99\x81",
    '8-)' => "\xf0\x9f\x98\x8e",
    '8-O' => "\xf0\x9f\x98\xaf",
    ':-(' => "\xf0\x9f\x99\x81",
    ':-)' => "\xf0\x9f\x99\x82",
    ':-?' => "\xf0\x9f\x98\x95",
    ':-D' => "\xf0\x9f\x98\x80",
    ':-P' => "\xf0\x9f\x98\x9b",
    ':-o' => "\xf0\x9f\x98\xae",
    ':-x' => "\xf0\x9f\x98\xa1",
    ':-|' => "\xf0\x9f\x98\x90",
    ';-)' => "\xf0\x9f\x98\x89",
    // This one transformation breaks regular text with frequency.
    //     '8)' => "\xf0\x9f\x98\x8e",
    '8O' => "\xf0\x9f\x98\xaf",
    ':(' => "\xf0\x9f\x99\x81",
    ':)' => "\xf0\x9f\x99\x82",
    ':?' => "\xf0\x9f\x98\x95",
    ':D' => "\xf0\x9f\x98\x80",
    ':P' => "\xf0\x9f\x98\x9b",
    ':o' => "\xf0\x9f\x98\xae",
    ':x' => "\xf0\x9f\x98\xa1",
    ':|' => "\xf0\x9f\x98\x90",
    ';)' => "\xf0\x9f\x98\x89",
    ':!:' => "\xe2\x9d\x97",
    ':?:' => "\xe2\x9d\x93",
);

或更好的分类显示:

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[':)'] = "\xF0\x9F\x8E\x85";

}, 6 );

感谢Ismael Miguel的全球评论,我相应地重写了这些摘录。

这里是新创建的车票#35905彼得·古森,对于一个新的smilies_trans过滤器。

更新-WordPress 4.7+

新的过滤器将在WordPress 4.7+中可用,但名称 smilies不是smilies_trans

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

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

    return $smilies;
} );

或明确地与:

add_filter( 'smilies', function( $smilies )
{
    $smilies[':)'] = "\xf0\x9f\x98\x8e";

    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[':)'] = "\xF0\x9F\x8E\x85";

    return $smilies;
} );

我们不再需要弄乱全局$wpsmiliestrans数组了!


表情符号会适合所有字体吗?
rob_st

奇怪,我的标签页和PC之间的笑脸看起来有多不同。:-)无论如何,跳动,也不喜欢使用全局变量进行修改,但这似乎是最好的选择。我认为我们可能会考虑在核心中添加一个适当的过滤器,该过滤器可用于过滤笑脸而不是更改糟糕的全局对象。;-)
Pieter Goosen

1
火车票#35905已提交。让食指获得合适的过滤器
Pieter Goosen

1
怎么add_action( 'init', function() use (&$wpsmiliestrans){ $wpsmiliestrans[':)'] = "\xf0\x9f\x98\x8e"; }, 6 );
Ismael Miguel

1
这是一个承诺-实际上,我use在此处的答案中使用了很多关键字,但这对全局变量有很好的提醒,再次感谢(:) <-也许我们可以使用对称的笑脸为每个人提供更好的可访问性@IsmaelMiguel
birgire

3

根据有关使用表情符号WordPress法典

将您想要的具有相同名称的图像上载到服务器(例如,在wp-content / images / smilies中),并将其放入主题的function.php中:

add_filter('smilies_src','my_custom_smilies_src',10,3);
   函数my_custom_smilies_src($ img_src,$ img,$ siteurl){
       返回$ siteurl。'/ wp-content / images / smilies /'.$ img;
   }
这样会将http://example.com/wp-includes/images/smilies/icon_question.gif替换为http://example.com/wp-content/images/smilies/icon_question.gif


不确定Codex演示为何假设10个输入参数,而不是3个?但是我认为这被表情符号覆盖了。
birgire

1
我想这是优先考虑的,所以顺序似乎不正确。我在食典中进行了更改,希望我们没有记错它:-)
rob_st

我已在法典中更正了您的编辑,并在答案中更正了您的代码。允许的参数应为3,将其设置1$img$siteurl将使您的代码失败 ,将被忽略,因此将在您的过滤器中未定义它:-)
Pieter Goosen
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.