I investigated the PHP code of the “Hello Dolly” WordPress plugin to find out what it does.
What is the WordPress Plugin Hello Dolly
Hello Dolly is a plugin that comes pre-installed with WordPress, and it displays a random excerpt from the lyrics of Louis Armstrong’s song on the administration screen.
The description of the WordPress plugin is as follows:
This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from Hello, Dolly in the upper right of your admin screen on every page.
Understanding the Behavior from the Source Code of “Hello Dolly”
There is only one PHP file used in “Hello Dolly.” Let’s read it to learn more about the plugin.
The beginning of the file contains comments with Plugin Name
, Plugin URI
, Description
, Author
, Version
, and Author URI
. At least Plugin Name
is required for the plugin.
Within this file, there are three functions: hello_dolly_get_lyric
, hello_dolly
, and dolly_css
.
hello_dolly_get_lyric
-
This function selects and returns one line from the lyrics of “Hello Dolly.” It uses
mt_rand
to get a random index. The functionwptexturize
is used to format the text according to WordPress standards and is still available in WordPress 5.The lyrics contain the line “Dolly’ll never go away” twice, and there are other duplicate lines as well. While
mt_rand
outputs a random line, it does not remove duplicates, so those duplicate lines have a higher chance of being selected. hello_dolly
-
This function retrieves one line from
hello_dolly_get_lyric
, converts it into HTML, and displays it. The returned HTML is shown on WordPress pages within ap
tag with anid
attribute ofdolly
. hello_css
-
This function displays the CSS applied to the displayed message as the content of the
style
tag. The functionis_rtl
determines whether the language used is right-to-left or left-to-right, adjusting the display accordingly.
In addition to these function definitions, two actions are added within the file.
add_action('admin_notices', 'hello_dolly');
-
This specifies that the
hello_dolly
function should be executed on the action eventadmin_notice
. Theadmin_notice
refers to the timing of displaying notifications in the admin area, including messages such as “Plugin activated.” add_action('admin_head', 'dolly_css');
-
This specifies that the
dolly_css
function should be executed on the action eventadmin_head
. Theadmin_head
is an event that occurs during the generation of thehead
tag when opening the admin area, and any text output here will be placed inside thehead
tag.
Extending the Plugin
Now that we understand how to use a simple plugin, let’s think about modifying the code to create a new plugin.
For instance, it would be interesting to display messages from “Mai-ni-chi, Shuzo! Kokoro o genki ni suru honki no ouen messeiji” based on the current date.
To obtain the date in PHP, you can use the following code to get the date in the Tokyo timezone.