Problem with shortcode to show user name – requires page refresh to work
-
The code below is an attempt to create a shortcode to display the user name that I have placed in the header. It mostly works, but when going to a new page, the logged in user is not found by the shortcode, and shows ‘Not logged in’, unless I do a manual refresh.
function custom_shortcode_func()
{
ob_start();
$current_user = wp_get_current_user();
if ( is_user_logged_in() )
{
$current_user = wp_get_current_user();
echo $current_user->display_name;
}
else
{
echo 'Not logged in';
}
$output = ob_get_clean();
return $output;
}
add_shortcode('current_user', 'custom_shortcode_func');I have tried messing around with various cache plugins, turning the cache on/off, etc, but did not have any luck. The website link is for a simple test setup to demonstrate the problem. It was created on Bluehost with the latest WordPress and php.
Thanks in advance for any help or suggestions.
The page I need help with: [log in to see the link]
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.