So I have a drop-down select that changes a custom field's value in their user profile based on their selected city. If a user chooses 'Detroit' for example, Detroit is then stored in their profile and that data is then used to redirect the customer based on their selection.
An issue I am running into seems to be if the customer selects a NEW different city on the homepage, or on a page deeper in the directory they get met with the TOO MANY REDIRECTS loop.
I understand WHY, I just can't seem to see where my issues are that are causing it.
Any help appreciated. Even if there is an easier, cleaner way to accomplish this.
add_action('wp_footer' , 'city_redirects');
function city_redirects() {
global $current_user;
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
wp_get_current_user(); // wordpress global variable to fetch logged in user info
$userID = $current_user->ID; // logged in user's ID
$metalocation_display = get_user_meta($userID, 'location_select', true); // stores the value of logged in user's meta data for 'test'.
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
//chicago REDIRECTS
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: example.com/chicago/' . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com/' . str_replace('chicago', '', 'example.com/chicago') . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && is_front_page() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com/');
}
//detroit REDIRECTS
if ( is_user_logged_in() && $metalocation_display == 'detroit' && strpos($url,'detroit') == false) {
header('Location: example.com/detroit/' . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && $metalocation_display !== 'detroit' && strpos($url,'detroit') == true) {
header('Location: example.com/' . str_replace('detroit', '', 'example.com/detroit') . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && is_front_page() && $metalocation_display !== 'detroit' && strpos($url,'detroit') == true) {
header('Location: example.com/');
}
//philadelphia REDIRECTS
if ( is_user_logged_in() && $metalocation_display == 'philadelphia' && strpos($url,'philadelphia') == false) {
header('Location: example.com/philadelphia/' . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && $metalocation_display !== 'philadelphia' && strpos($url,'philadelphia') == true) {
header('Location: example.com/' . str_replace('philadelphia', '', 'example.com/philadelphia') . $_SERVER['REQUEST_URI']);
}
if ( is_user_logged_in() && is_front_page() && $metalocation_display !== 'philadelphia' && strpos($url,'philadelphia') == true) {
header('Location: example.com/');
}
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.