wordpress snippet add login/logout button in header

This code adds a login/logout button to the primary menu of the site. If the user is logged in, the code adds a “Logout” link to the menu. If the user is not logged in, it adds a “Login” link.

To customize the appearance of the login/logout button, you can modify the HTML and CSS of the links that are generated by the code. For example, you can add custom CSS classes to the links, or wrap the links in custom HTML tags to style them with CSS.

function add_login_logout_link($items, $args) { if ($args->theme_location == 'primary') { if (is_user_logged_in()) { $items .= '<li><a href="'.wp_logout_url().'">Logout</a></li>'; } else { $items .= '<li><a href="'.wp_login_url().'">Login</a></li>'; } } return $items; } add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);

Leave a Reply

Your email address will not be published. Required fields are marked *