I need to hand code an affiliate link (we do not want to use an existing plugin). I do not do a lot of coding, so bear with me.

It seems so simple I have a link: http://www.domain.com/?code=8765

Then I want to grab that code number and put it in a cookie. That way if a lead form goes in it can be passed through the API to SalesForce. Can anyone tell me why this isn't working? If the id variable is set to a string it works fine.

Does this even make sense to accomplish this using this method?

add_action( 'init', 'set_agent_cookie' );

function set_agent_cookie() {
    $name = 'agent';
    $id = $_GET['code'];
       setcookie( $name, $id, time() + 3600, "/", COOKIE_DOMAIN );
  • just tested, your code works perfectly here even with numbers...
    – benomatis
    Oct 18 '16 at 20:29
  • I may be wrong, but I think this would over write the code if you browse to a page without the variable in the URL. I think you will need to add some logic to only set the cookie if the variable is set.
    – JpaytonWPD
    Feb 19 '17 at 14:21
  • if-then statement to make it a bit better (as solution) can be found here: wordpress.stackexchange.com/questions/257046/…
    – Malachi
    Jun 4 '19 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.