I'm trying to redirect users when they visit my page template template-logout.php
, but I keep getting the Wordpress "Do you really want to logout?" question despite wpnonce being generated. What am I doing wrong?
Here's an example of the type of URL being generated by my function below when I reach that WP confirmation page:
https://localssl:8890/wp-login.php?action=logout&redirect_to=https%3A%2F%2Flocalssl%3A8890&_wpnonce=752dc07192
CODE
template-logout.php
<?php
/**
* Template name: Log Out
*/
get_header();
// Logic handled via functions.php
get_footer(); ?>
functions.php
// Redirects for template pages
add_filter('template_redirect', 'osu_redirect_on_logout');
function osu_redirect_on_logout() {
if ( is_page_template( 'templates/template-logout.php' ) ) {
$logout_url = wp_logout_url( home_url() );
wp_safe_redirect( $logout_url ); // Logout and Redirect to homepage
exit;
}
}
** EDIT **
Answer below by Gabriel is the better way of doing this, I was getting muddled with the redirection side of things. To make sure this method redirects to the homepage, this was my final code:
// Redirect to homepage when using wp_logout();
add_action( 'wp_logout', 'osu_redirect_homepage_after_logout');
function osu_redirect_homepage_after_logout(){
wp_redirect( home_url() );
exit();
}
// Redirects for template pages
add_filter('template_redirect', 'osu_redirect_on_logout');
function osu_redirect_on_logout() {
if ( is_page_template( 'templates/template-logout.php' ) ) {
wp_logout();
}
}