25% off ProSNIPC25OFF

Time-Based Greeting in the Admin Bar

Displays a personalised Good morning/afternoon/evening greeting with the current user's display name in the WordPress admin bar.

PHPby SnipCraft
php
<?php
add_action( 'admin_bar_menu', 'scseed_admin_bar_greeting', 8 );
if ( ! function_exists( 'scseed_admin_bar_greeting' ) ) {
    function scseed_admin_bar_greeting( $wp_admin_bar ) {
        if ( ! is_admin_bar_showing() ) {
            return;
        }
        $user = wp_get_current_user();
        if ( ! $user->exists() ) {
            return;
        }
        $hour = (int) current_time( 'G' );
        if ( $hour < 12 ) {
            $salutation = __( 'Good morning', 'scseed' );
        } elseif ( $hour < 17 ) {
            $salutation = __( 'Good afternoon', 'scseed' );
        } else {
            $salutation = __( 'Good evening', 'scseed' );
        }
        $wp_admin_bar->add_node( array(
            'id'    => 'scseed_greeting',
            'title' => esc_html( $salutation . ', ' . $user->display_name . '!' ),
            'href'  => admin_url( 'profile.php' ),
            'meta'  => array( 'title' => esc_attr__( 'View your profile', 'scseed' ) ),
        ) );
    }
}
#admin#admin-bar#dashboard#personalization#ux