functions – Logout redirect via page template without confirmation?

Question

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;
    }
}

0
Osu 3 weeks 2023-01-03T02:52:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse