Skip to content
Advertisement

WordPress – How to add condition “if user logged in” for html/css/js menu

I’m working with elementor, in the html widget I wrote some code to create a menu and it works fine. Now I want to hide a part of the menu to make it visible only to logged in users. I understand that this is done with php, but elementor’s html widget does not support php. So I’m wondering if there is an alternative to php or if there is a way to indirectly apply php to my menu ?

I’m working with elementor, in the html widget I wrote some code to create a menu and it works fine. Now I want to hide a part of the menu to make it visible only to logged in users. I understand that this is done with php, but elementor’s html widget does not support php. So I’m wondering if there is an alternative to php or if there is a way to indirectly apply php to my menu?

Here is my test menu, it’s just a test so there are only two items. Can anyone help me understand how I can accomplish this? Sorry but I’m new and I’m trying to learn.

I appreciate any response, thanks.

Edit with possible solution: in wp-content / themes / child-theme / I created a folder called custom_menu, inside this I added two files, menu_structure.php and menu_style.css. The php file contains the html code with the script, while in the css I inserted the styles.

Then I went to the functions.php file and added this line include_once get_theme_file_path (‘/custom_menu/menu_structure.php’); This is to make wordpress understand that menu_structure.php must execute the functions, otherwise the shortcode would not work.

Finally, in the menu_structure.php file I added a shortcode, inside it I then added html menu and script. That’s how…

JavaScript

This is the initial code that I have inside elementor html widget

JavaScript
JavaScript
JavaScript

Advertisement

Answer

(css hacks may hide the menu item, but it will still be available to everyone whether logged in or not).

I don’t use wordpress, but it’s method for checking/returning login status seems to be is_user_logged_in(). See wordpress documentation. You would enclose the section you wish to hide in a conditional with that method.

I haven’t tested the following, but it would look something like this:

JavaScript

There is probably a plugin that would allow you to add php, probably within a <script> tag or something.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement