Very simple geo targeting for WordPress - php

I need to do some very basic geo-targeting for a WordPress site.
Something like:
<?php if ( visitor is from usa ): ?>
<?php get_footer('us'); ?>
<?php else: ?>
<?php get_footer('other-countries'); ?>
<?php endif; ?>
Till now I used the GEO service provided by ip-api.com.
My code looks like this:
<?php $ip = $_SERVER['REMOTE_ADDR'];
$query = #unserialize(file_get_contents('http://ip-api.com/php/' . $ip));
$geo = $query['countryCode'];
if( $query['countryCode'] == 'US') : ?>
DO THIS
<?php else: ?>
DO THAT
<?php endif ?>
The problem is that the php unserialize is now deprecated and extremely slow. Ip-api suggest to use JSON instead. But I am a total newbie and I don't know how to achieve the same results with JSON. Can someone please help me?
I know there are various plug-ins out there, but I think they are overkill for the very simple geo targeting I need.
PS: I just learn that I should use the code
$.getJSON( '//ip-api.com/json?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});
But still I need help to put together the final complete code.

Try this:
<?php
$ip = ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) ?
$_SERVER['REMOTE_ADDR'] : '';
$country_code = '';
if ( $ip ) {
// 'fields' are comma-separated list of fields to include in the response.
// No spaces between the fields. See http://ip-api.com/docs/api:returned_values#field_generator
$url = 'http://ip-api.com/json/' . $ip . '?fields=countryCode,status,message';
$res_body = wp_remote_retrieve_body( wp_safe_remote_get( $url ) );
$geo = json_decode( $res_body );
$country_code = isset( $geo->countryCode ) ? $geo->countryCode : '';
}
if ( 'US' === $country_code ) : ?>
DO THE THIS
<?php else : ?>
DO THE THAT
<?php endif; ?>
PS: I just learn that I should use the code
Yes, that's an example of doing it with jQuery.

Related

create a function and modify a post on the wordpress site based on the post id

I'd want to edit the content of one of my posts using this way, but it's not working.
Does this filter affect the php output or the raw php file?
add_filter( 'the_content', 'multiple_string_replacements');
function multiple_string_replacements ( $contentt ) {
if ( is_single('15467') && in_the_loop() && is_main_query() ) {
$condition = true;
if($condition){
$urlsdothejob = "link.com";
$text = array(
"$variable1" => "$urlsdothejob",
"$variable2" => "$urlsdothejob",
);
$contentt = str_ireplace(array_keys( $text ), $text, $contentt);
};
}
return $contentt;
}
If you know the conditions that you're looking for, you should be able to write an IF statement to match.
if ($constantvalue == "anothervalue") {
// make local changes
}
Can you give an example of what you're trying to do? It's hard to understand the request with such little information.
Try like this hope its working for you.
while(have_posts()):
$ID = get_the_ID();
$data1 = get_post_meta($ID,'meta_key',true);
$data2 = get_post_meta($ID,'meta_key',true);
if($data1 == $data2) {
update_post_meta($ID,'meta_key','meta_value');
}
endwhile;

PHP - If an array contains part of a string

I have the following PHP code:-
<?php
if( have_rows('postcode_checker', 'option') ):
while ( have_rows('postcode_checker', 'option') ) : the_row(); ?>
<?php
$postcodes .= get_sub_field('postcodes');
$postcode_url = get_sub_field('page');
?>
<?php endwhile;
else : endif;
$postcode_array = $postcodes; // This collects postcodes, i.e. LE67, LE5 etc...
$postcode_array = explode(',', $postcode_array);
$postcode_array = str_replace(' ', '', $postcode_array);
$postcode_search = $_POST['postcode']; // This will be a single postcode i.e. LE67
if (in_array($postcode_search, $postcode_array)) {
echo 'yes';
} else {
echo 'no';
}
?>
So the code above is working fine if I want to look up say LE67 and it finds LE67 in the array and returns 'yes'. Now if I search LE675AN for example it will return no even though it needs to be returning yes as it is within the postcode area.
Any idea's on how I can achieve this?

php forwarding linkage from foreach to result

<?php
if(isset($_POST['MarkaSinifi'])) {
$MarkaSinifi = $_POST['MarkaSinifi'];
echo "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption[1]=2&customfield[10]=webx";
foreach($MarkaSinifi as $marka) {
echo '&configoption[' . $marka . ']=1';
}
}
?>
I made that code for redirect user every $marka has a value like 1,2,3 this is the result for the link i want to redirect: "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption[1]=2&customfield[10]=webx&configoption[51]=1&configoption[52]=1"
This is just text, but i want to redirect users to that link. How can i do that?
This is what i did: https://coinearn.shop/marka.php
If you know all index of $marka, you can use
<?php
header('Location: '. "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption$marka[0]=2&customfield[10]=webx&configoption$marka[1]=1&configoption$marka[2]=1");
This can help too
<?php
$args[0] = 'Location: https://www.xxxxx.com/cart.php?a=add&pid=3&configoption%s=2&customfield[10]=webx&configoption%s=1&configoption%s=1';
$args = array_merge($args, $marka);
header(call_user_func_array('sprintf', $args);

How to add another page ID in a PHP function - newbie asking?

I need some help to see where I am going wrong.
I am trying to add a page ID to this original function:
<?php if( $post->ID != '91' )
{
get_sidebar();
} ?>
>
to also exclude the ID 1267.
I am trying this, with no success.
<?php
$pageIDs_to_exclude=array("91","1267");
if( $post->ID != $pageIDs_to_exclude )
{
get_sidebar();
}
?>
Surely there must be a better way of doing this? Or what am I missing?
Thnaks for any help
/Anders
$pageIDs_to_exclude = array("91","1267");
// in_array will return false if it doesn't find $post->ID within the $pageIDs_to_exclude array
if( ! in_array($post->ID, $pageIDS_to_exclude) )
{
get_sidebar();
}
You are trying to directly compare $post->ID to $pageIDs_to_exclude, an array. As $post->ID is not an array (it is a string), this is not possible. Instead, see if $post->ID is in $pageIDs_to_exclude.
if (!in_array($post->ID, $pageIDs_to_exclude)) {
get_sidebar();
}
in_array() is a function that will return true if the object is found in the array.
You can use in_array of php. It will return true or false.
$pageIDs_to_exclude=array("91","1267");
if(!in_array($post->ID,$pageIDs_to_exclude))
{
get_sidebar();
}
Use the PHP function in_array() (http://php.net/manual/en/function.in-array.php) to search for a value in an array:
<?php
$page_ids = array("91", "1271");
if(!in_array($post->ID, $page_ids))
{
get_sidebar();
}
?>

Check session is set, codeigniter

I have a upload script which post upload lets the user edit the content title for a period of time. It sets the following in the ci_sessions user_data column in the db:
array (
'user_data' => '',
'edit' =>
array (
'image_id' => 'HF',
'session_id' => '783c15b057bcd9c19d3fd82f367ee55d',
),
)
The problem is my session CHECK code can't find the session:
<?php if ($this->session->userdata('edit') !== FALSE) : ?>
<?php echo '<!-- session found -->'; ?>
<?php $session_info = $this->session->userdata('edit'); ?>
<?php $ids_array = explode(",", $session_info['image_id']); ?>
<?php foreach ($ids_array as $id): ?>
<?php
if ($id == $alpha_id
&&
$session_info['session_id'] == $this->session->userdata('session_id')) :
?>
The echo on line 2 of that block never gets outputted.
Can anyone see what I'm doing wrong? thanks
heres my controller http://pastebin.com/aXeRn1VN
Try this
<?php if( $this->session->userdata('edit') ) : ?>
instead of
<?php if ($this->session->userdata('edit') !== FALSE) : ?>

Categories