Using WordPress ‘convert_smilies()’ PHP function

The convert_smilies() WordPress PHP function converts the text equivalent of smilies into their corresponding images.

Usage

Here’s a typical usage of the convert_smilies() function:

echo convert_smilies("Hello, world! :)");

In this example, the text smiley :) will be converted into an image.

Parameters

  • $text (string, required): This is the content in which smilies are to be converted from text to images.

More Information

See WordPress Developer Resources: convert_smilies()

This function only converts smilies if the ‘use_smilies’ option is true and the global used in the function isn’t empty. Be sure to check these conditions before using this function.

Examples

Example 1

Explanation: Display a message with a smiley as an image.

echo convert_smilies("This smiley is going to show as an image... :)"); // Smiley will be converted to an image

Example 2

Explanation: Make your plugin generated content parse emoticons.

add_filter( "my_plugin_content_handle", "convert_smilies" );
echo apply_filters( "my_plugin_content_handle", "Howdy! ;) Generated with <3 by My Custom Plugin" ); // Smiley will be converted to an image

Example 3

Explanation: Convert multiple smilies in a string.

echo convert_smilies("I'm happy :) and sad :("); // Both smilies will be converted to their corresponding images

Example 4

Explanation: Use in a post content.

$my_post_content = "It's a sunny day! :)";
echo convert_smilies($my_post_content); // Smiley will be converted to an image

Example 5

Explanation: Use with a comment text.

$my_comment_text = "Great post! :D";
echo convert_smilies($my_comment_text); // Smiley will be converted to an image