Created
March 11, 2024 06:25
-
-
Save ipokkel/b57fba8cd49f66100a11ac1fd8d975ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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