Skip to content

Instantly share code, notes, and snippets.

@justingreerbbi
Last active June 11, 2024 11:40
Show Gist options
  • Save justingreerbbi/768f1effcca69b4098c9d0f7731deba0 to your computer and use it in GitHub Desktop.
Save justingreerbbi/768f1effcca69b4098c9d0f7731deba0 to your computer and use it in GitHub Desktop.
Logout user using WP REST API WordPress Custom Endpoint
/**
* Head to https://wp-oauth.com for more info on user authentication and custom login and out solutions
* for WordPress
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'wpoauthserver/v1', '/logout/', array(
'methods' => 'GET',
'callback' => 'wp_oauth_server_logout'
) );
} );
function wp_oauth_server_logout() {
wp_logout();
wp_redirect('https://redirect-location.com');
exit;
}
// Call https://site.com/wp-json/wpoauthserver/v1/logout/
@justingreerbbi
Copy link
Author

myplugin can be anything you need it to be. Typically this is the custom name for your custom API.

Be sure to change the redirect URL in the wp_redirect function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment