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

时间:2016-02-22 作者:krish

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

enter image description here

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

2 个回复
SO网友:birgire

将:)的表情符号改写为

内容表情将通过以下方式转换:

add_filter( \'the_content\', \'convert_smilies\' );
在哪里this partconvert_smilies() 功能很重要:

$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:

插件可以通过设置$wpsmiliestrans 对于数组,使用blogger键入的代码键和图像文件的值。

这是全球$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",
);
或更好的K排序显示:

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 对于全球comment, 我相应地重写了片段。

这是newly created#35905 通过Pieter Goosen, 关于新的smilies_trans 滤器

更新-WordPress 4.7+

新过滤器将available 在WordPress 4.7+,但它的名称将是smiliessmilies_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 阵列不再存在!

SO网友:rob_st

根据WordPress Codex on using smilies:

将你想要的同名图片上传到你的服务器上(比如wp-content/images/smilies),并将其放入你的主题功能中。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;
   }
这将取代http://example.com/wp-includes/images/smilies/icon_question.gif 具有http://example.com/wp-content/images/smilies/icon_question.gif

相关推荐

注释中的Emoji疑难解答

为了利用新添加的表情符号功能,刚刚将我的本地和实时安装的一个辅助项目更新为4.2。表情符号在我的本地机器上运行得很好,但它们似乎不能作为实时版本的评论。Local帖子标题中的表情符号-工作帖子内容中的表情符号-工作评论中的表情符号-工作Live<帖子标题中的表情符号-工作中的表情符号-工作中的表情符号-评论中的表情符号-不工作中的表情符号Troubleshooting<主题-尝试激活215;不起作用</插件-尝试停用所有插件;不起作用</主题+插件-尝试激活215个插件并停用所有插