Log in and out using custom pages, no logout confirmation and redirects for logging in and out 2023

congrats, you got most of it right!

But I suggest you check out the wp_logout_url() function. That’s the proper way to log out WP. This function returns the URL that allows the user to log out of your site. It will handle the nonce creation for you, so you don’t need to worry about that.

Modifying your code to handle that is pretty easy, here’s an example :

function change_menu($items){
    foreach($items as $item){
        if( $item->title == "Logout"){
            $redirect_url="https://google.com"; // your custom redirect URL
            $item->url = wp_logout_url($redirect_url);
    return $items;
add_filter('wp_nav_menu_objects', 'change_menu');

Here, the wp_logout_url($redirect_url) function will create a correct URL for logging out, and then redirecting to the $redirect_url which you specify. This should help avoid the logout confirmation page.