'If statement in .twig file to check if user is logged into Wordpress not rendering expected output to display login button vs. logout button
I am trying to create a login/logout button for a WordPress site that uses a plugin to only display the content to members who are registered on our WordPress site. According to what I've found I should be able to do that with the timber user object {% if user %}. I am learning as I go with timber/twig for WordPress as I've inherited the website that was originally outsourced and built by an external company.
I found a solution at https://wordpress.stackexchange.com/questions/257332/how-to-check-in-timber-if-user-is-loggedin/257333 which should work in theory but the browser is only displaying the code inside the else. I don't believe I need to add anything to the controller file to render this but I could be wrong.
<nav class="utility-nav">
<button class="header-search__toggle" aria-label="Toggle Search Form"><i class="fa fa-search"></i> Search</button>
{% if user %}
<a class="header__login-link" href="/wp-login.php?action=logout&_wpnonce=6e24015e99">Log Out »</a>
{% else %}
<a class="header__login-link" href="/login/">Log In »</a>
{% endif %}
</nav>
I am expecting to see the Login button when not logged into WordPress and the logout button when logged in. I am only seeing the login button regardless if I'm logged in or not.
Edited to add what's in my controller php file, I did try adding it to the context array with no luck, but if it's a timber object then I shouldn't need to add anything here:
$context = array(
'logo_url' => get_site_url(),
'nav' => wp_nav_menu( array(
'container' => false,
'menu_class' => 'nav',
'echo' => false,
'theme_location' => 'header',
)),
'search' => get_search_form(false),
);
if (empty($context)) return;
Timber::render('header-hr.twig', $context);
Solution 1:[1]
I decided to go a different route and do it all in the controller php:
$login = function() {
if ( is_user_logged_in() ) {
return '<a class="header__login-link" href="/wp-login.php? action=logout&_wpnonce=6e24015e99">Log Out »</a>';
} else {
return ' <a class="header__login-link" href="/login/">Log In »</a>';
}
};
$context = array(
'logo_url' => get_site_url(),
'nav' => wp_nav_menu( array(
'container' => false,
'menu_class' => 'nav',
'echo' => false,
'theme_location' => 'header',
)),
'search' => get_search_form(false),
login' => $login()
);
if (empty($context)) return;
Timber::render('header-hr.twig', $context);
With this in twig file:
<nav class="utility-nav">
<button class="header-search__toggle" aria-label="Toggle Search Form"><i class="fa fa-search"></i> Search</button>
{{login}}
</nav>
All is well and working now!
Solution 2:[2]
This is how I'm outputting the logout link next to the comments form:
<a href="{{ function('wp_logout_url', post.link ) }}">Log out</a>.
It'll log the user out and redirect them back to the blogpost.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stacy |
| Solution 2 | Fabien Snauwaert |
