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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php /** * @package Hello_Dolly * @version 1.6 */ /* Plugin Name: Hello Dolly Plugin URI: http://wordpress.org/extend/plugins/hello-dolly/ Description: 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 <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page. Author: Matt Mullenweg Version: 1.6 Author URI: http://ma.tt/ */ function hello_dolly_get_lyric() { /** These are the lyrics to Hello Dolly */ $lyrics = "Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong We feel the room swayin' While the band's playin' One of your old favourite songs from way back when So, take her wrap, fellas Find her an empty lap, fellas Dolly'll never go away again Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong We feel the room swayin' While the band's playin' One of your old favourite songs from way back when Golly, gee, fellas Find her a vacant knee, fellas Dolly'll never go away Dolly'll never go away Dolly'll never go away again"; // Here we split it into lines $lyrics = explode( "n", $lyrics ); // And then randomly choose a line return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] ); } // This just echoes the chosen line, we'll position it later function hello_dolly() { $chosen = hello_dolly_get_lyric(); echo "<p id='dolly'>$chosen</p>"; } // Now we set that function up to execute when the admin_notices action is called add_action( 'admin_notices', 'hello_dolly' ); // We need some CSS to position the paragraph function dolly_css() { // This makes sure that the positioning is also good for right-to-left languages $x = is_rtl() ? 'left' : 'right'; echo " <style type='text/css'> #dolly { float: $x; padding-$x: 15px; padding-top: 5px; margin: 0; font-size: 11px; } </style> "; } add_action( 'admin_head', 'dolly_css' ); ?> |
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.
1 2 3 4 5 6 7 8 9 |
$dateTime = new DateTime(); $dateTime->setTimeZone(new DateTimeZone('Asia/Tokyo')); $year = (int) $dateTime->format('Y'); $month = (int) $dateTime->format('m'); $day = (int) $dateTime->format('d'); $hour = (int) $dateTime->format('H'); $minute = (int) $dateTime->format('i'); $second = (int) $dateTime->format('s'); |