Archived topic
Show block element on in certain countries
4 replies · Started by Max on November 26, 2022
Hi there!
I've a block element that I want to add to a few pages, but only to visitors from a specific country.
Would this be possible with a setting or some kind of php snippet?
Thanks!
I've tried this, but that didn't work:
add_filter( 'generate_element_display', function( $display, $element_id ) {
global $post;
if ( 34988 === $element_id && ( is_page() && $post->post_parent == '34994' ) ) {
if (geoip_country_code_by_name($_SERVER['REMOTE_ADDR']) == 'DE') {
$display = true;
}
}
return $display;
}, 10, 2 );
Id of the element = 34988
Page I want to show it on = 34994
Hi Max,
I'm not aware of an easy way to get the user's geolocation.
It will require custom development or a plugin.
Too bad! I thought it would be possible to make some kind of IF statement to check the country.
Woocommerce has that function built-in, if you are using Woocommerce it might be worth a try to contact their support.
Otherwise, a plugin or custom development will be required.