Q:

How to execute some code BEFORE logout in WordPress

I am trying to build a custom logout page for a WordPress site.
I disabled the wp-login.php using the following .htaccess code.
<Files wp-login.php>
deny from all
</Files>

If you access this you will receive a 403 Forbidden response.
Now I want to execute a function "BEFORE" the user logs out.
function execute_this() {
  wp_logout();
}

The thing is all suggestions from the internet is using the following code.
add_action('wp_logout', 'execute_this');

But this hook executes "AFTER" successful logout which in my case this can't be done since wp-login.php is inaccessible.
That is why I'm trying to log out the user "BEFORE" it goes to the wp-login.php file.
Is there a hook/action that triggers this kind of scenario?
I also tried doing such a thing (customizing my logout page with the page that I created):
add_filter( 'logout_url', 'wpse_58453_logout_url' );
function wpse_58453_logout_url( $default )
{
    return plugin_dir_url(__FILE__) . 'logout.php';
}

Then in my logout.php
require_once('../../../wp-includes/pluggable.php');
wp_logout();

I know this is the wrong approach since executing a PHP file directly is never a good idea plus I can't access WordPress hooks when doing something like this.
Is there any way to get around this?

A:

I'm not super experienced with wordpress, but one of the sites that I help maintaining has a custom logout url (/tp-logout) and the theme has the following code to perform the logout
function tp_custom_logout() {
        if ( is_page( 'tp-logout' ) && is_user_logged_in() ) {
                wp_destroy_current_session();
                wp_clear_auth_cookie();
                $url_logout = home_url();
                wp_redirect( $url_logout );
                exit();
        }
}

add_action( 'template_redirect', 'tp_custom_logout' );

It looks extremely similar to the default WP logout function, so no idea why it was added, but maybe you can add your custom code following this approach.
I would call the function wp_logout() to do the actual logout rather than copy the code (and not calling the wp_logout action hooks), to make it a bit more friendly.

