Skip to content

Instantly share code, notes, and snippets.

@alfredo-wpmudev
Last active May 23, 2024 04:58
Show Gist options
  • Save alfredo-wpmudev/3cdcfeaac09e9cdd2e6825db74536378 to your computer and use it in GitHub Desktop.
Save alfredo-wpmudev/3cdcfeaac09e9cdd2e6825db74536378 to your computer and use it in GitHub Desktop.
Custom redirect for guest visitors and in logout.
<?php
/**
* Plugin Name: Custom Guess Redirector
* Description: Redirect guess visitor to the Home Page or custom page, same after user logout will be redirected to a custom page.
* Version: 1.0.0
* Author: Alfredo Galano Loyola
* Author URI: https://wpmudev.com
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/*
Hook and function to redirect to the sepcif page you need to redirect,
edit the value of $redirect
*/
add_action('template_redirect','redirect_not_logged_users',999);
function redirect_not_logged_users(){
//Edit this variable to the page you want to redirect.
$redirect_to = 'https://myblank.tempurl.host';
if(!is_front_page()){
if(!is_redirect_excluded_page()){
if(!is_user_logged_in()){
wp_redirect($redirect_to);
exit();
}
}
}
}
/*
Function that allow create exclusion list and check if the current URL is excluded.
*/
function is_redirect_excluded_page() {
//Add or remove the slug of the pages you want to exclude, follow the same format, they need to be sepparated by comma and using '' symbols.
$excluded_pages = array('wp-login.php', 'wp-register.php','westeros');
return in_array($GLOBALS['pagenow'], $excluded_pages);
}
/*
Logout Redirect, to customize it need to replace the '/' for the slug of the page you want to redirect.
*/
add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
function logout_without_confirm($action, $result)
{
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : '/';
$location = str_replace('&amp;', '&', wp_logout_url($redirect_to));
wp_redirect($location);
die;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment