Random User Greeting

tdrayson

Looking to give your users a fun and unique greeting?

This function outputs a random greeting along with the logged-in username.

PHP

        <?php

function tct_hello_user(){
    if (is_user_logged_in()) {
        $morning = ["morning matey", "Top of the morning"];

        $afternoon = ["Afternoon", "How was lunch"];

        $evening = ["You should be asleep", "Sleep well"];

        $hour = date("H");
        $username = wp_get_current_user()->user_login;

        if ($hour < 12) {
            $random = array_rand($morning);
            return $morning[$random] . " " . $username;
        } elseif ($hour > 11 && $hour < 18) {
            $random = array_rand($afternoon);
            return $afternoon[$random] . " " . $username;
        } elseif ($hour > 17) {
            $random = array_rand($evening);
            return $evening[$random] . " " . $username;
        }
    }
}
      
Copy

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise