Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active October 26, 2020 00:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neilgee/d32c06eadbbea460adda2aa7ff00b87e to your computer and use it in GitHub Desktop.
Save neilgee/d32c06eadbbea460adda2aa7ff00b87e to your computer and use it in GitHub Desktop.
Login/Logout Shortcode
<?php //<~ remove if using in functions.php
add_shortcode( 'login_logout', 'login_logout' );
/**
* Add a login/logout shortcode button
* @since 1.0.0
*/
function login_logout() {
ob_start();
if (is_user_logged_in()) :
// Set the logout URL - below it is set to the root URL
?>
<a role="button" href="<?php echo wp_logout_url('/'); ?>">Log Out</a>
<?php
else :
// Set the login URL - below it is set to get_permalink() - you can set that to whatever URL eg '/whatever'
?>
<a role="button" href="<?php echo wp_login_url(get_permalink()); ?>">Log In</span></a>
<?php
endif;
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment