Skip to content

Cookie Monster – WordPress URL Parameter Utility

I recently had a situation in which I needed to pass a URL parameter into WordPress so that it could be inserted into all links off of a blog page.  For example, a partner would pass traffic to a blog using the format  All links embedded in the post would need to pass the value of refcode if it existed, even if the visitor requested multiple pages after the initial visit.

I created a Plugin (Cookie Monster) that will allow a Blog Admin to define which URL parameters should be captured, and then creates a shortcode so that the value can be used in your Posts and Pages.  A WordPress shortcode is a macro code that is embedded in your content.

In the example above, if you define refcode in the Cookie Monster Admin Settings, and if that URL parameter refcode contains a value in the querystring, it will automatically be saved into a cookie in the visitors browser. Anywhere that you place the shortcode [refcode] in your Post, Cookie Monster will insert the querystring value into your Post.  The cookie value will persist across browser sessions.

If you click on this link, you will see that the querystring value refcode is now [refcode]

Try changing refcode= in the link and reload this page.  Let me know if you have any further ideas for expanding this plugin.

You can download the latest copy here.

If you want to be able to build plugins like this yourself, click here and more).



  1. Lutz wrote:

    Thanks for sharing, very useful! You just saved me one or two hours of coding :)

    Tuesday, January 19, 2010 at 3:14 pm | Permalink
  2. casey wrote:

    Is there any way to use this in themes?

    Monday, February 1, 2010 at 7:57 am | Permalink
  3. casey wrote:

    I think I may have answered this myself.

    echo $_COOKIE[get_option('cookie_param1')];

    Monday, February 1, 2010 at 8:16 am | Permalink
  4. gsmith wrote:

    Yes Casey, that should work Let me know if it did, and send us a link to your site so we can check it out. Thanks!

    Friday, February 5, 2010 at 12:36 pm | Permalink
  5. sandy wrote:

    Over my head, I do try to learn new techy things, but didn’t understand this.

    Saturday, February 20, 2010 at 11:35 am | Permalink
  6. Al wrote:


    This is EXACTLY what I was looking for! Thank you so much.

    I have an odd error message on my site when I use it though:

    Warning: Cannot modify header information - headers already sent by (output started at /home/salesex/public_html/wp-content/themes/Delericon/single.php:7) in /home/salesex/public_html/wp-content/plugins/cookiemonster/Cookie.php on line 17.

    Any ideas how I address this?

    Monday, March 1, 2010 at 8:32 am | Permalink
  7. gsmith wrote:

    I have seen that error before. It is usually because your theme is sending output before CookieMonster is called. Look for any spaces before php in single.php. If you still cannot figure it out, let me know and I will send you an email address that you can send your file to me and I will take a look at it.

    Tuesday, March 2, 2010 at 12:17 pm | Permalink
  8. Al wrote:

    gsmith – You are exactly on the money. I had gaps in my header file that clearly only were brought to light when I installed CookieMonster.

    Thanks for your reply – I really appreciate it!


    Tuesday, March 2, 2010 at 1:36 pm | Permalink
  9. Chris wrote:

    Very helpful plug-in, Gary. When I pass multiple parameters, the values are stored in the cookie but the page won’t display. It generates a 404 error.

    What’s the right way to pass
    multiple params?

    Tuesday, March 23, 2010 at 1:02 am | Permalink
  10. gsmith wrote:

    Chris, I am not sure I follow your issue. When you say multiple parameters, do you mean like,refcode=CookieMonster&refcode2=cookie2. This works fine on this page, where are you getting the 404? Try this and you will see the second parameter value at the bottom of the blog post:

    Friday, March 26, 2010 at 2:06 pm | Permalink
  11. Chris wrote:

    There seems to be a built-in limitation with WP processing very long URLs. It fails somewhere when the URL is too long and/or too many parameters are passed. This happens regardless of using the plug-in or not.

    Unfortunately, I’m using it with Aweber and the default option to pass params to the thank you page blows past the WP limitation. Then the 404 error appears.
    .-= Chris´s last blog ..Headlight signup process =-.

    Friday, March 26, 2010 at 6:08 pm | Permalink
  12. Superb Site – Thanks for the info

    Saturday, April 3, 2010 at 7:30 pm | Permalink
  13. Kathy wrote:

    This works brilliantly for my outgoing links and passing a hidden variable in a form. Thanks you!
    .-= Kathy´s last blog ..Avoiding Soreness when Starting Weight Training for Women =-.

    Monday, April 19, 2010 at 4:03 pm | Permalink
  14. ryan wrote:

    Does it work with seo friendly urls ? like ?

    With normal link in wordpress like$var3=sth

    I can get vars with $_GET[‘var_name’];

    but with premalinks (post_id/post-name) it doesn’t work and it shows error 404

    Thursday, April 22, 2010 at 8:10 am | Permalink
  15. gsmith wrote:

    Yes Ryan, it works with Permalinks. The example on this post is a permalink. Let me know if you have any problems getting it to work.

    Tuesday, April 27, 2010 at 12:03 pm | Permalink
  16. ryan wrote:

    Thanks for reply. But I have already used tips from

    Tuesday, April 27, 2010 at 7:24 pm | Permalink
  17. Liam wrote:

    Your plugin looks excellent and very useful. I just installed it on WP 3.0 and get the following error message:

    options-general.php?page=CookieMonster, You do not have sufficient permissions to access this page.

    Is the plugin compatible with wp 3.0. Is there something I can change in the plugin php file to make this work?

    Thanks for your time,

    Saturday, June 26, 2010 at 7:06 am | Permalink
  18. Joe Hornback wrote:

    When I hit attempt to go into cookie monster admin section, it tells me “You do not have sufficient permissions to access this page.” I am the admin with full permission…

    Thursday, July 8, 2010 at 7:29 pm | Permalink
  19. Peter wrote:

    Anyone found a way to fix it? Seems like a perfect little app but can’t use it :( WP3 is showing “You do not have sufficient permissions to access this page.” when going to settings “wp-admin/options-general.php?page=CookieMonster”

    Monday, July 19, 2010 at 5:24 am | Permalink
  20. bronstar wrote:

    To fix the error “You do not have sufficient permissions to access this page” change the following line in Cookie.php:

    add_options_page(“Cookie Monster Options”, “Cookie Monster”, 1, “Cookie Monster”, “CookieAdmin”);


    add_options_page(“Cookie Monster Options”, “Cookie Monster”, 1, “Cookie-Monster”, “CookieAdmin”);

    (the difference is to add a hyphen between Cookie and Monster in the 2nd last parameter)

    Sunday, July 25, 2010 at 6:32 am | Permalink
  21. Mikkel wrote:

    Maybe it’s just me, but where do I find the shortcodes …?

    If I go to the admin page and add new vars, all it says is this:

    “URL Parameter 1 : ex: affcode – This cookie now set to ”

    Looks like something is missing …?

    Wednesday, August 11, 2010 at 9:10 am | Permalink
  22. Meison wrote:

    I can’t get it to work with premalinks as well (post_id/post-name) it doesn’t work and it shows error 404. Need to mofidy .htaccess ? Pls help.

    Sunday, August 15, 2010 at 2:27 pm | Permalink
  23. anantha wrote:

    Custom Permalink structure is : /%postname%/%post_id%/?refCode=[refCode]

    Although refCode is in browser cookies and works well inside posts (by pasting [refcode]), it doesnt work in permalink

    Please clarify

    Friday, August 20, 2010 at 8:11 am | Permalink
  24. gsmith wrote:

    Can Meison or Anantha send me an example link? I cannot seem to duplicate what you are seeing.

    Friday, August 20, 2010 at 12:02 pm | Permalink
  25. JOS wrote:

    Is there a way of clearing the cookies, other than passing blank parameters via the url that is?

    Wednesday, August 25, 2010 at 12:55 pm | Permalink
  26. michah wrote:

    I’m a newbie and a bit unclear
    I’m getting the same thing as Mikkel here above

    Sunday, August 29, 2010 at 11:31 am | Permalink
  27. michah wrote:

    Now, When I hit attempt to go into cookie monster admin section, it tells me “You do not have sufficient permissions to access this page.” I am the admin with full permission…
    I checked and there’s a hyphen as you suggested previously.

    Tuesday, August 31, 2010 at 3:13 am | Permalink
  28. gsmith wrote:

    Michah, make sure that you have the most recent version, it should have fixed the permissions issue with WP 3.0.
    For you and Mikkel, after you go to the admin page and create a URL parameter, until you set that cookie by placing ?refcode=something on the querystring, that value will be blank. Hope that makes sense. Let us know if you have any further issues.

    Tuesday, August 31, 2010 at 11:56 am | Permalink
  29. Shannon wrote:

    can someone show me an example for writing a url using this plugin?

    I got the plugin installed but I cannot figure out how to get it to write my url.

    param1 = a
    param2 = v
    param3 = i

    Saturday, September 4, 2010 at 2:34 am | Permalink
  30. Mikkel wrote:


    I’m still baffled. On some of my pages, it works, and on others it does not.

    Could you explain what you mean when you say I need to “set that cookie by placing ?refcode=something on the querystring”?

    Case in point, I have added a variable ‘gallery’ in the Cookie Monster settings, and I have called a page with “?gallery=foobar” appended to the URL – shouldn’t that work …? It doesn’t. I can’t access the ‘foobar’ value on the page, and in the Cookie Monster settings it still just says “URL Parameter 3 : gallery – This cookie now set to”

    Do I need to treat the variable in any special way on my page? I use $_GET[‘gallery’] to retrieve the value, but it comes out blank …

    Tuesday, September 7, 2010 at 10:19 am | Permalink
  31. Mikkel wrote:

    Okay – I’m closer to the root of the problem now: It doesn’t work on pages that are password-protected!

    If I remove the protection, everything is fine, but I would like to avoid that.

    Is this a known bug or limitation?

    Wednesday, September 8, 2010 at 10:48 am | Permalink
  32. Thomas wrote:

    Maybe I am missing something, but when I want to use the refcode to store a variable, and then have that variable (keyword) displayed in a post, what code will I use?

    Thursday, September 30, 2010 at 3:52 am | Permalink
  33. Aaron wrote:

    I can’t figure out what exactly to type in the settings parameter input box.

    And I don’t know what to write as the shortcode to put in my page. Any chances you could set me on the right track?

    Thursday, October 7, 2010 at 12:27 pm | Permalink
  34. Jeff wrote:

    This is brilliant. Solved two tricky features my client wanted in one! Thanks and thanks all foor the good support.

    Friday, October 15, 2010 at 3:34 pm | Permalink
  35. AJ wrote:

    I agree with AARON. I can not figure this out. Is there a way someone can do a Video on this real quick to help figure this out.

    Tuesday, November 2, 2010 at 5:39 pm | Permalink
  36. DennisD wrote:

    Thomas/Aaron/AJ, I felt the same way, then I figured it out.

    Go into Settings > Cookie Monster, and enter the variable name you with to use. Then in your code, you’d place the shortcode (same name as variable name). Example, if I had a variable called refid, I’d enter it into the Cookie Monster config, then in my code, I’d do the following:

    <a href="

    Hope this helps.

    Tuesday, November 16, 2010 at 8:24 am | Permalink
  37. I’m very happy to have found this plug-in, but I too have landed the dreadful 404 error.

    I know that the variables were passed and cookie set, because if I return to the admin page it tells me the value of my four variables.

    It seems to have the problem if I try to pass more than one variable to through the URL. i.e. ?month=3 is fine, but ?month=3&day=12 throws up 404 error.

    Friday, December 3, 2010 at 11:09 pm | Permalink
  38. I have been looking all over for how to answer this question for a plugin I am building. This seems to do the trick in general, but does not work within the plugin. I have a static page that I would like to display info based on the ID passed in the URL , for example As you can see in the second link, the playerID is picked up and displayed using the shortcode on the page, but the variable is not available for use in the plugin. I am using the following in my code as was mentioned in an earlier comment:

    $player_id = $_COOKIE[get_option(‘playerID’)];

    $player_id is later used in an SQL query to return the information related to the player. any advice is much appreciated.

    Monday, December 27, 2010 at 7:54 pm | Permalink
  39. Carlos wrote:

    I am having a similar issue like Jason.

    The plugin reads the first refcode just fine but any subsequent variable is not picked up.

    I DO NOT get a 404 page when I use multiple refcodes. The only current issue is that only the first refcode is stored as a cookie.

    Any help would be appreciated.

    This is a fantastic plugin that I have been looking for a while. Hopefully you will continue to support and make it better.

    Tuesday, February 22, 2011 at 2:24 pm | Permalink
  40. Andy wrote:

    Any chances i can inplment it on my none WP website?

    Tuesday, October 11, 2011 at 7:38 pm | Permalink
  41. saijin wrote:

    The parameter is not adding on every link on a page, specially on wp_nav_menu.

    Can you please give me a hint on how can I fix it?

    Tuesday, October 11, 2011 at 9:51 pm | Permalink
  42. Martin wrote:

    Hi there

    Awesome little plugin, but I have one question. If you use the [shortcode] on a page and a person refers with an ID, then it is great, but what if you want to have a default value in the shortcode in case no referal code is given?

    Tuesday, November 1, 2011 at 3:11 pm | Permalink
  43. Martin wrote:

    Hi there

    Awesome little plugin, but I have one question. If you use the [shortcode] on a page and a person refers with an ID, then it is great, but what if you want to have a default value in the shortcode in case no referal code is given?



    Tuesday, November 1, 2011 at 3:11 pm | Permalink
  44. elie wrote:

    also, how can we add more url parameters? right now, I can only add four

    Wednesday, November 23, 2011 at 4:22 pm | Permalink
  45. Rafael wrote:

    Hello Smith,

    As the sixth comment, which AL was Monday, March 1, 2010 at 8:32 am
    My problem is the same. Using the theme OptimizePress and is giving the error:
    “Warning: Cannot modify header information – headers already sent by (output started at /home/suces362/public_html/ in /home/suces362/public_html/ on line 17″

    I’ve tried to follow the step single.php but could not solve the problem, what do I do?


    Sunday, December 11, 2011 at 3:29 am | Permalink
  46. gsmith wrote:

    Latest version 1.3 allows you to create a default for each parameter. Give it a try and let me know how it works.

    Saturday, December 17, 2011 at 2:43 am | Permalink
  47. I was recommended this website by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my difficulty. You’re amazing! Thanks!

    Saturday, February 4, 2012 at 10:26 am | Permalink
  48. Brian M wrote:

    I used to be able to pass the cookie value into a form field by placing this inside the field value:

    But now I can’t get that to work. Any ideas?

    Saturday, February 4, 2012 at 6:35 pm | Permalink
  49. Brian M wrote:

    I used to be able to pass the cookie value into a form field by placing this inside the field value:



    But now I can’t get that to work. Any ideas?

    Saturday, February 4, 2012 at 6:36 pm | Permalink
  50. Brian M wrote:

    actually, I just figured out a new way:



    Replace ref with whatever your cookie parameter is.

    Saturday, February 4, 2012 at 6:44 pm | Permalink
  51. blog network wrote:

    It is truly a nice and helpful piece of info. I am glad that you simply shared this helpful information with us. Please stay us up to date like this. Thank you for sharing.

    Tuesday, March 6, 2012 at 2:09 pm | Permalink
  52. Casey wrote:

    Is there any reason why wpautop is added to this plugin?

    Saturday, March 24, 2012 at 8:47 pm | Permalink
  53. Tal wrote:

    Thanks Gary!

    Saturday, June 16, 2012 at 7:42 am | Permalink
  54. Frank wrote:

    Not sure if this comment string is still active… but I have a question about passing MULTIPLE variables and how the href html should be coded.

    Example: I set up 2 variables to pass…

    1) id -> affiliate id
    2) c -> campaign id

    I embed those variables into the href code (to create a live link) and separate the 2 variables with & as follows…..

    Here is the how the html href is coded:


    It works great IF there ‘are’ 2 variables… say id=123 and c=facebook

    …then it pulls up live link like this: -> perfect!

    BUT… if there is ‘only ONE’ variable say id=123 and c is NOT identified at all in the original URL …then the href link pulls up this site:;

    The ‘&’ sign sticks on the end…

    Is there a better/correct way to do this so that the href html does not pull up the & sign when there is NO 2nd (or 3rd, 4th…multiple) variables?

    Thank you!

    Monday, November 26, 2012 at 2:07 pm | Permalink
  55. Cynthia wrote:

    It seems as though on most computers the cookie does not set anymore … the cookie works on the original page … but when you surf to other pages it goes back to the default ?? Any suggestions

    Friday, December 7, 2012 at 1:09 pm | Permalink
  56. Alan wrote:

    what if the referral id is in front of the domain


    Saturday, June 15, 2013 at 8:39 pm | Permalink
  57. Roman wrote:

    I get a PHP Notice in logs:

    PHP Notice: Undefined index: from in /var/www/ on line 19

    In line 19 of the file I have:

    $g_refcode = $_COOKIE[$cookie];

    Can you please help to fix this ?

    Thank you,

    Saturday, December 28, 2013 at 4:27 am | Permalink
  58. Shayn wrote:

    This plugin doesn’t seem to work well with optimizepress. It interrupts the video on the page. any ideas on why this happens or how to get around it?


    Wednesday, January 29, 2014 at 4:50 pm | Permalink
  59. Amy wrote:

    Can this plugin be used with Optimize Press 2?

    Tuesday, February 25, 2014 at 1:43 pm | Permalink
  60. JOhn wrote:

    What about adding the ability to pass the variable to a custom field in an autoresponder…Say aweber…So that when I mail leads I can actual mail the referers id

    Tuesday, August 5, 2014 at 3:47 pm | Permalink

2 Trackbacks/Pingbacks

  1. CookieMonster-plus • Mercs LLC on Sunday, September 26, 2010 at 10:27 pm

    […] gsmith’s CM page, you can change the value of the example parameter as passed to this page and watch this page […]

  2. Worpress Plugin Modification on Tuesday, April 26, 2011 at 4:11 pm

    […] […]

Post a Comment

Your email is never published nor shared. Required fields are marked *