If you are just trying to logout a user and redirect them back to the homepage, could you just use the wp_logout()
function?
So instead of this:
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;
}
Your logic could just be:
if ( is_page_template( 'templates/template-logout.php' ) ) {
wp_logout();
}