Function to log out of a password protected page

If you want to add a logout button to your password protected page, add this code in your plugin or function file:

<?php
add_action( 'display_logout_button', 'wpster_logout_button' );
function wpster_logout_button() {
	?>       
	<form method="post"> 
		<input type="submit" name="logout" value="Déconnexion"/> 
	</form>
	<?php
	if(isset($_POST['logout'])) { 
		setcookie( 'wp-postpass_' . COOKIEHASH, stripslashes( '' ), time() - 864000, COOKIEPATH, COOKIE_DOMAIN );
		wp_redirect( home_url() );
		exit;
	}
}
?>

To display the button, add this code to your template file:

<?php
do_action( 'display_logout_button' );
?>

Submit a Comment

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