Welcome Message for users
-
Hi Everyone!
I’m trying to create a simple welcome message that displays in the header when a user logs in. Something like Welcome (user)!
I tried to use the following code, but I’m getting a syntax error:
function displayName () {
global $user_identity;
get_currentuserinfo();
echo “Welcome Back” . $user_identity;Any thoughts on what I might be doing wrong to accomplish this?
Thanks
John
-
It might help if you posted the specific error, but first of all I notice that you’re missing the closing function brace – I’m not sure if this was just a copy error, but you’ll want to fix this if it exists in the actual code:
function displayName () { global $user_identity; get_currentuserinfo(); echo "Welcome Back" . $user_identity; }Secondly, you might want to reconsider your use of the
get_currentuserinfo()function, which is currently deprecated. Instead, use thewp_get_current_user()function.function displayName () $user = wp_get_current_user(); echo "Welcome Back" . $user->display_name; }Thirdly, as this plugin simply executes snippets, you’ll need to make sure that you are actually calling your function to have it do something. You can achieve this by creating a shortcode which you then later use in a post or page, or hooking it to a theme-specific hook, if the theme you use supports them:
function wporg_welcome_message() $user = wp_get_current_user(); echo "Welcome Back" . $user->display_name; } // use the [welcome_message] shortcode in a post or page add_shortcode( 'welcome_message', 'wporg_welcome_message' ); // this won't work unless you change the action hook to something supported by your theme add_action( 'theme_below_header', 'wporg_welcome_message' );
The topic ‘Welcome Message for users’ is closed to new replies.