- This topic has 7 replies, 3 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
February 4, 2023 at 8:19 am #2521339
Jörg
I would like to display the name of the logged-in user in a widget in the sidebar. If nobody is logged in, the widget should not be displayed. How can I realize this with GeneratePress?
Many thanks!
February 4, 2023 at 9:03 am #2521371David
StaffCustomer SupportHi there,
you could create yourself a simple Shortcode by adding a PHP Snippet to your site:
add_shortcode('hello_user', function(){ $html = ''; $prefix = 'Message before username: '; global $current_user; wp_get_current_user(); if ( is_user_logged_in() ) { $html = $prefix . $current_user->display_name; } return $html; });This doc explains how to add PHP: https://docs.generatepress.com/article/adding-php/
Then you can add the
[hello_user]shortcode to your sidebar widget or using a GP Element.February 4, 2023 at 11:31 am #2521472Jörg
Thank’s for your reply 🙂
The shortcode works. But if user logged out, the widget ist also visible as a “placeholder” (https://tanzsport-schwedt.de, 4. Widget on right sidebar). It shold not be visible, if user logged out.
Thank you for your help!
February 4, 2023 at 4:13 pm #2521605Ying
StaffCustomer SupportHi Jorg,
Should all 4 widgets not appear when the user’s logged out?
Does that mean you don’t need a sidebar when the user’s logged out?
February 5, 2023 at 1:09 am #2521841Jörg
The sidebar should not be deactivated when the user is logged out. Just the widget that displays the name of the logged in user.
Similar to the function of the Woocommerce shopping cart widget, which is only displayed if there is at least one item in it.
February 5, 2023 at 4:54 am #2521967David
StaffCustomer SupportWhere in the sidebar will the widget be displayed?
If it is at the top or the bottom of the sidebar, then instead of using a Widget, use a Block Element:https://docs.generatepress.com/article/block-element-hook/
In the list of hooks, there are ones for the sidebars, before and after.
You can add whatever content you want there including the shortcode.
And then set the elements display rules -> User to Logged in.Let me know if thats a solution
February 5, 2023 at 7:11 am #2522065Jörg
It work’s 🙂 Thank you!
February 5, 2023 at 7:31 am #2522080David
StaffCustomer SupportGlad to hear that!
-
AuthorPosts
- You must be logged in to reply to this topic.