Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created March 11, 2024 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/b57fba8cd49f66100a11ac1fd8d975ee to your computer and use it in GitHub Desktop.
Save ipokkel/b57fba8cd49f66100a11ac1fd8d975ee to your computer and use it in GitHub Desktop.
<?php
/**
* Redirect to the referrer after logout.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function wp_redirect_to_referrer_after_logout() {
global $pmpro_pages;
$referrer = wp_get_referer();
// if the referrer is the login page, redirect to home
if ( strpos( $referrer, 'wp-login' ) !== false || ( ! empty( $pmpro_pages['login'] ) && strpos( $referrer, get_permalink( $pmpro_pages['login'] ) ) !== false ) ) {
wp_safe_redirect( home_url() );
exit;
} else {
// Redirect to the referrer after logout.
wp_safe_redirect( $referrer ? $referrer : home_url() );
exit;
}
}
add_action( 'wp_logout', 'wp_redirect_to_referrer_after_logout' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment