9

If you enter :) in WordPress, it automatically replaces it with:

enter image description here

Is there a way to use a different smiley for the :)

1
  • Hi krish, could you provide more details on what you've tried already (and why it didn't work)?
    – kraftner
    Feb 22, 2016 at 12:32

2 Answers 2

15

Overriding the emoji of :) to 😎

The content smilies are converted with:

add_filter( 'the_content', 'convert_smilies' );

where this part of the convert_smilies() function is of importance:

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

If we peek into translate_smiley() then we find the following:

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

before the smilies_src filter is applied.

So this filter isn't available in the case of the :) smiley.

We have the smilies initialized with:

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

and within the function description for smilies_init() we can read the 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.

Here's the global $wpsmiliestrans array:

$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",
);

or the nicer ksorted display:

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-)] => 😎
)

So if I correctly understand the above core comment, then we could do the following:

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

}, 6 );

but this only works for predefined smiley keys, for the $wp_smiliessearch to work.

But I don't like this suggested approach, modifying the global array! Hopefully there's another one better!

Demo plugin - 🎅

I tried to come up with an application for this. I'm not sure if this already exists, but here it is:

<?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 );

Thanks to Ismael Miguel for the global comment, I rewrote the snippets accordingly.

Here's the newly created ticket #35905 by Pieter Goosen, regarding a new smilies_trans filter.

Update - WordPress 4.7+

The new filter will be available in WordPress 4.7+, but it's name will be smilies not smilies_trans.

Our above examples can be written as:

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

    return $smilies;
} );

or explicitly with:

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

    return $smilies;
} );

The demo plugin becomes:

<?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;
} );

We don't need to mess around with the global $wpsmiliestrans array anymore!

10
  • Would the smilies work with every font then?
    – rob_st
    Feb 22, 2016 at 16:50
  • Strange how different the smileys look between my tab and pc. :-) Anyways, jip, also don't like modifying around with globals, but this seems like the best you are going to get out of this deal. I think we might look into adding a proper filter in core which we can use to filter smileys instead of altering a crappy global. ;-) Feb 22, 2016 at 17:57
  • 1
    trac ticket #35905 submitted. Lets cross fingers that we get a proper filter Feb 22, 2016 at 18:41
  • 1
    How about add_action( 'init', function() use (&$wpsmiliestrans){ $wpsmiliestrans[':)'] = "\xf0\x9f\x98\x8e"; }, 6 );? Feb 22, 2016 at 19:15
  • 1
    It's a promise - actually I use the use keyword a lot in my answers here, but it's a good reminder regarding the globals, thanks again (:) <-- maybe we could use a symmetric smiley for better accessability for everyone @IsmaelMiguel
    – birgire
    Feb 22, 2016 at 20:13
3

According to the 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

3
  • Not sure why the Codex demo is assuming 10 input arguments, instead of 3? But I think this is overriden by the Emoji's.
    – birgire
    Feb 22, 2016 at 14:11
  • 1
    I suppose it is meant to be the priority, so the order seems incorrect. I changed it in the Codex, hoping we didn't get it wrong :-)
    – rob_st
    Feb 22, 2016 at 15:11
  • I have corrected your edit in the codex and corrected your code in your answer. The allowed arguments should be 3, setting it to 1 will fail your code as $img, $siteurl will be ignored and will therefor be undefined in your filter :-) Feb 22, 2016 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.