I need to set 1 cookie, saving a data sent from a form.

I see that I have to hook my setcookie() to the 'init'. That's OK.

add_action( 'init', 'cookie_function');

My 'cookie_function' will have $cookieName, $cookieValue, $time, COOKIEPATH, COOKIEDOMAIN.

If I need something like setcookie('test', $_POST['test']), what do I call:

cookie_function() //and where do I pass parameters?


setcookie() //and this function will be related to my cookie_function()?
  • whaT is the main reason to set the cookie?
    – prosti
    Commented Jan 16, 2017 at 19:42
  • I have to save a referral code which contains the user's friends who sent you the link to the blog (I ask that in a particular page of the site), I really need this thing on my blog. Commented Jan 16, 2017 at 19:52

2 Answers 2


You use setcookie() like any other function, with parameters as described in the Codex.

How you would use it in a form would usually depend on how and when the $_COOKIE variable needs to be made accessible.

One common method, if you want a change in the DOM both to register immediately and to persist, is to use a Javascript/jQuery function whose effect is duplicated in the PHP functions that will draw upon $_COOKIE variables. The cookie will also typically be set or updated in the same script, commonly with the aid of jquery-cookie or js-cookie.


A cookie is name and value pair simplified. Use cookies to store data on the client side.

In PHP you would define cookie like this:

setcookie( name, value, expire, path, domain, secure, httponly);

Cookies can be stolen via JavaScript but they may not be. It depends.

The httponly flag, if set to true, means cookies cannot be altered via JavaScript.

The secure flag purpose is to prevent clear text cookies over HTTP. By setting the secure flag, the browser will prevent the transmission of a cookie over an unencrypted channel.

The other parameters like domain and path are constraints, that precisely set where cookie should work.

For instance setting cookie domain to www.example.com will mean only the exact domain www.example.com will be matched, while .example.com will also match any subdomain (forum.example.com, blog.example.com).

The path argument default value of / means every request will get the cookie, while /forum/ limits the cookie to just /forum path.

The expire flag is usually set to some period like 1 or 2 weeks meaning that the cookie will last for 1 or 2 weeks.

I see that I have to hook my setcookie() to the 'init'. That's OK.


You don't need to use 'init' hook to set the cookie. This is not a must. Wherever you work with your form data in your code you simply use either $_POST['test'] or $_GET['test'] based on the form type. Also, no need to mention you need to check these variables first.

Later you use something like this to check if the cookie is present.

if ( isset( $_COOKIE['name'] ) ) ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.