Using WordPress ’emoji_ext’ PHP filter

The emoji_ext WordPress PHP filter allows you to modify the extension of emoji PNG files in your WordPress site.

Usage

add_filter( 'emoji_ext', 'your_custom_function' );

function your_custom_function( $extension ) {
    // your custom code here
    return $extension;
}

Parameters

  • $extension (string) – The emoji extension for PNG files. Default is .png.

More information

See WordPress Developer Resources: emoji_ext

Examples

Change emoji extension to .webp

To serve emoji files with the .webp extension instead of .png:

add_filter( 'emoji_ext', 'change_emoji_extension' );

function change_emoji_extension( $extension ) {
    $extension = '.webp';
    return $extension;
}

Add a custom suffix to the emoji extension

To add a custom suffix to the emoji extension, for example .custom.png:

add_filter( 'emoji_ext', 'add_custom_suffix_to_emoji_extension' );

function add_custom_suffix_to_emoji_extension( $extension ) {
    $extension = '.custom.png';
    return $extension;
}

Conditionally change emoji extension

To change the emoji extension based on user’s device:

add_filter( 'emoji_ext', 'conditionally_change_emoji_extension' );

function conditionally_change_emoji_extension( $extension ) {
    if ( wp_is_mobile() ) {
        $extension = '.mobile.png';
    } else {
        $extension = '.desktop.png';
    }
    return $extension;
}

Change emoji extension based on a user’s role

To change the emoji extension based on a user’s role:

add_filter( 'emoji_ext', 'change_emoji_extension_based_on_user_role' );

function change_emoji_extension_based_on_user_role( $extension ) {
    $current_user = wp_get_current_user();
    if ( in_array( 'administrator', $current_user->roles ) ) {
        $extension = '.admin.png';
    } else {
        $extension = '.non-admin.png';
    }
    return $extension;
}

Change emoji extension for logged-in users

To change the emoji extension only for logged-in users:

add_filter( 'emoji_ext', 'change_emoji_extension_for_logged_in_users' );

function change_emoji_extension_for_logged_in_users( $extension ) {
    if ( is_user_logged_in() ) {
        $extension = '.loggedin.png';
    }
    return $extension;
}