Xenforo 2.1 How to Show Xenforo Banner only to One country with IF Condition

Belinux

Active member
Registered
Joined
Aug 17, 2021
Messages
37
Points
18

Reputation:

I was seeking an code with if condition in which I can display any text, notice, or any html code in shape of banner or any advertisement only for specific country. For Example what I am looking for that if any users comes on my site from USA Country, than that banner or text/html code only shows to that user, if user is not from USA than it shows different text.

Can anyone please guide me and send me any working if conation statement for that purpose, all efforts are highly appreciated by anyone who can contribute to get this.
 

Snister2

Active member
Registered
Joined
Jan 6, 2023
Messages
43
Points
18

Reputation:

you must have API ACCESS for that way with Geolocation.

Code:
<img id="country-image" style="display:none;" alt="Country Specific Image">

JavaScript:
document.addEventListener("DOMContentLoaded", function() {
    fetch('https://ipinfo.io/json?token=YOUR_TOKEN_HERE')
        .then(response => response.json())
        .then(data => {
            const country = data.country;
            const imageElement = document.getElementById('country-image');

            switch(country) {
                case 'US':
                    imageElement.src = 'path/to/us-image.jpg';
                    imageElement.style.display = 'block';
                    break;
                case 'IN':
                    imageElement.src = 'path/to/india-image.jpg';
                    imageElement.style.display = 'block';
                    break;
                // Add more countries as needed
                default:
                    imageElement.src = 'path/to/default-image.jpg';
                    imageElement.style.display = 'block';
                    break;
            }
        })
        .catch(error => {
            console.error('Error fetching the country information:', error);
            document.getElementById('message').textContent = 'Error loading content';
        });
});
just add cases for more country example: case 'PH' for philippines.
 

Belinux

Active member
Registered
Joined
Aug 17, 2021
Messages
37
Points
18

Reputation:

you must have API ACCESS for that way with Geolocation.

Code:
<img id="country-image" style="display:none;" alt="Country Specific Image">

JavaScript:
document.addEventListener("DOMContentLoaded", function() {
    fetch('https://ipinfo.io/json?token=YOUR_TOKEN_HERE')
        .then(response => response.json())
        .then(data => {
            const country = data.country;
            const imageElement = document.getElementById('country-image');

            switch(country) {
                case 'US':
                    imageElement.src = 'path/to/us-image.jpg';
                    imageElement.style.display = 'block';
                    break;
                case 'IN':
                    imageElement.src = 'path/to/india-image.jpg';
                    imageElement.style.display = 'block';
                    break;
                // Add more countries as needed
                default:
                    imageElement.src = 'path/to/default-image.jpg';
                    imageElement.style.display = 'block';
                    break;
            }
        })
        .catch(error => {
            console.error('Error fetching the country information:', error);
            document.getElementById('message').textContent = 'Error loading content';
        });
});
just add cases for more country example: case 'PH' for philippines.
Snister2Can you please give some idea about a text/html message instead of image, and if without API please.
 

Snister2

Active member
Registered
Joined
Jan 6, 2023
Messages
43
Points
18

Reputation:

Can you please give some idea about a text/html message instead of image, and if without API please.
BelinuxYes but it won't provide the user's country directly. Instead, it provides the user's latitude and longitude, which you can then use to infer the country. However, this approach might not be as accurate or straightforward as using a dedicated geolocation service.
 

Belinux

Active member
Registered
Joined
Aug 17, 2021
Messages
37
Points
18

Reputation:

Can you extract the code which works behind
Options > Communication > Notices > Open any Notice > Page Criteria > current Date and Time > In the Selected Zone

there I can guess we can got the point and code which will work and needed. we can set time zone as well instead of country. Please figure out that thing.
 
Top