Login / Logout Link Shortcode
Registers a [login_logout_link] shortcode that shows "Log in" to visitors and "Log out" to logged-in users, returning them to the same page.
Opens WPIDE on
Code
add_shortcode( 'login_logout_link', function () {
$current = get_permalink();
$current = $current ? $current : home_url( '/' );
if ( is_user_logged_in() ) {
return '<a class="logout-link" href="' . esc_url( wp_logout_url( $current ) ) . '">Log out</a>';
}
return '<a class="login-link" href="' . esc_url( wp_login_url( $current ) ) . '">Log in</a>';
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.