skip to Main Content

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?

2

Answers


  1. 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');
    
    Login or Signup to reply.
  2. 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;
    
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search