How do I create a popup that saves a variable for a custom field?

[ad_1]

Hey all, beginner dev here and this is my first time using cookies and popups on a site.

 

I’m aiming to create a popup window when visitors come to my site so that they can select a region, and this region will be stored in a cookie while also being used as a value for a custom field for my site. Is this something that I can do easily manually with code, or should it be done via plugin?

 

I have that my manager suggested I look at from another site he assisted in building in the past. The code is used to store a cookie for referrals, and although he’s not a developer himself, he told me to reference the code as he thinks it will help me. To me, this looks overly complicated for what I’m trying to do as all I’m really doing is saving a selection and assigning it to a variable I’d like to access elsewhere. Is this code useless to me?

 

function setCookie(name, value, days) {
var expires = “”;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = “; expires=” + date.toUTCString();
}
document.cookie = name + “=” + (value || “”) + expires + “; path=/”;
}
function getCookie(name) {
var nameEQ = name + “=”;
var ca = document.cookie.split(‘;’);
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ‘ ‘) c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}

If anyone has any examples of cases like mine above that I can reference, that would also be a big help.

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer