0

I am trying to remove a button for all logged in users on my website. When logged out I have a register button on the start page - which I want to remove when a user log in. Not a menu item - a button on the start page.

I have tried this code in the child theme function file:

function example_function()
{
    if ( is_user_logged_in() ) 
    {
.hide-me {
display: none;
    }
}
add_action('init', 'example_function');
}

However this gives my site critical error.

.hide-me is a css class I use on the button when I edit in elementor page builder.

Not sure what I do wrong. Do you know and can you please help me?

1

2 Answers 2

3

Besides the perfect answer of @IT Goldman with PHP you can also achieve this using JavaScript or just css. WordPress adds a class 'logged-in' to the body tag when the user is logged in maybe try this CSS code

body.logged-in .hide-me{

  display: none;

}

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I liked the clean small code. But this does not work for some reason. Tried clearing cache. Added the code in the style.css in child theme and at the bottom of the code.
2

The general idea is good but You are mixing php and css. Also, since you are echo-ing some styles on the page, it's better to use wp_head hook. This assumes that the button has a css class called hide-me.

function example_function() {
    if (is_user_logged_in()) {
        echo '<style>.hide-me { display: none; }</style>';
    }
}
add_action('wp_head', 'example_function');

3 Comments

Thank you very much - this works! 👏 And thanks for letting me in with some extra knowledge. I am just at copy paste knowledge now - trying to understand more as I go. Many thanks 🙏
One extra question. Can I write the code in the function file in one line without space and enter etc? I want as small code as possible. Would this help loading the code so to speak?
Yes you can, but it would be less readable. I just use auto-formatter in my editor. It won't change one bit in terms of performance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.