I am currently working on a Wordpress site, one of the tasks was to create custom post location post types which I was able to do. I then used a filter I wrote in functions.php to include a separate locations.php file I wrote to be displayed on my location post types pages. See filter below:
add_filter('the_content', 'prepend_location_data' );
function prepend_location_data( $content ){
if( is_singular('location')){
$html=include("locations.php");
return $content . $html;
}
return $content;
}
I was able to then use the ACF plugin to add custom fields to my locations posts and display them using ACF functions on my locations.php page. My next task is to add some sort of widget or menu to the top of all the pages to display a particular phone number based on the users IP. I was able to write a php function within the locations.php file I made that gets the user's IP, uses the IP to get the city of the IP and then if that city is equal to a city included in one of the city custom field I created it will return the phone number for that city. See functions below:
<?php
function get_the_user_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//Checks if IP is from shared internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//Checks if IP is passed from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
//Most trustworthy source of IP address
$ip = $_SERVER['REMOTE_ADDR'];
}
//Uses ipinfo.io to find location information based on IP address
$details = json_decode(file_get_contents("https://ipinfo.io/{$ip}"));
//Returns city value from the details array
$city=$details->city;
return apply_filters('wpb_get_ip', $city );
}
function display_phone(){
$cityField=get_field('city');
$phoneField=get_field('phone_number');
//Assigns the return value of get_the_user_ip to a variable
$userCity=get_the_user_ip();
if($userCity==$cityField){
return ($phoneField);
}
}
add_shortcode('show_phone', 'display_phone');
?>
The problem is the shortcode will only work on the locations pages. I realize I somehow have to pass the return value of the display_phone function back to the functions.php file or maybe just do all this logic within the functions.php to begin with. Does anyone have any suggestions about things to try or where to look? Can I use ACF functions within my functions.php? Should I use a Javascript function to do this? Any advice is appreciated.
I also have tried the following modifications to the display_phone function to no avail.
function display_phone(){
global $post;
$current_id = $post->ID;
$cityField=get_field('city', $post->ID);
$phoneField=get_field('phone_number');
//Assigns the return value of get_the_user_ip to a variable
$userCity=get_the_user_ip();
if($userCity==$cityField){
return ($phoneField);
}
}
next modification to display_phone function:
function display_phone(){
global $post;
$current_id = $post->ID;
$cityField=get_field('city', $current_id);
$phoneField=get_field('phone_number');
//Assigns the return value of get_the_user_ip to a variable
$userCity=get_the_user_ip();
if($userCity==$cityField){
return ($phoneField);
}
}
UPDATE: Ok so after thinking about this a bit more i'm thinking I need to write a function that will loop through all of the city and phone number fields of the location post types to see if they match with the city returned by the get_the_user_ip function. Obviously the display phone function will have to be modified to do this and i'm thinking I will have to take both functions out of locations.php and maybe perform this logic within the header.php file. Does anyone have any experience doing something similar or any advice on how to do this? I'm not really sure how to loop through the field data of my location post types unless i'm actually doing the logic within a post. I realize this could be solved by using a hard-coded array of cities and phone numbers but I am trying to avoid that and make use of the ACF plugin instead.
UPDATE: So I took the logic out of the locations.php page and moved it to the end of the functions.php file. Right now it is printing the correct phone number at the top of every page on load but only for a second. So it looks like the next step in the process is to get this number assigned to a variable and passed through a function somehow so that I can then assign to a shortcode and use throughout my site.
<?php
function get_the_user_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//Checks if IP is from shared internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//Checks if IP is passed from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
//Most trustworthy source of IP address
$ip = $_SERVER['REMOTE_ADDR'];
}
//Uses ipinfo.io to find location information based on IP address
$details = json_decode(file_get_contents("https://ipinfo.io/{$ip}"));
//Returns city value from the details array
$city=$details->city;
return apply_filters('wpb_get_ip', $city );
}
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'location'
));
$userCity=get_the_user_ip();
if( $posts ):
foreach( $posts as $post ):
$cityField=get_field('city');
//echo "$cityField";
$phoneField=get_field('phone_number');
//echo "$phoneField";
if($userCity==$cityField){
echo ($phoneField);
}
endforeach;
wp_reset_postdata();
endif;
?>