Cookie Monster – WordPress URL Parameter Utility

 

cookie monster girl

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 http://www.myblog.com?refcode=joe.  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.

 

69 thoughts on “Cookie Monster – WordPress URL Parameter Utility

  1. casey

    I think I may have answered this myself.

    [code]
    <?php
    echo $_COOKIE[get_option('cookie_param1')];
    ?>
    [/code]

  2. gsmith Post author

    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!

  3. Al

    HI,

    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?

  4. gsmith Post author

    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.

  5. Al

    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!

    Al

  6. Chris

    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?

  7. Chris

    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 =-.

  8. gsmith Post author

    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.

  9. Liam

    Greetings,
    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,
    Liam

  10. Joe Hornback

    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…

  11. Peter

    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”

  12. bronstar

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

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

    UPDATED:

    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)

  13. Mikkel

    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 …?

  14. Meison

    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.

  15. anantha

    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

  16. gsmith Post author

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

  17. michah

    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.

  18. gsmith Post author

    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.

  19. Mikkel

    @gsmith:

    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 …

  20. Mikkel

    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?

  21. Pingback: CookieMonster-plus • Mercs LLC

  22. Thomas

    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?

  23. Aaron

    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?

  24. AJ

    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.

  25. DennisD

    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="http://www.mysite.com/index.php?refid=%5Brefid%5D

    Hope this helps.

  26. Jason Spencer

    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.

  27. Nick Collingham

    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 http://dev.flyingdogsbaseball.com/pages/player-info that I would like to display info based on the ID passed in the URL , for example http://dev.flyingdogsbaseball.com/pages/player-info?playerID=139. 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.

  28. Carlos

    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.

  29. Pingback: Worpress Plugin Modification

  30. saijin

    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?

  31. Martin

    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?

  32. Martin

    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?

    Thanks

    Martin

  33. Rafael

    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/superoportunidade.net/wp-content/themes/OptimizePress/squeezetemplate_001.php:3) in /home/suces362/public_html/superoportunidade.net/wp-content/plugins/cookiemonster/Cookie.php on line 17”

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

    Rafael

  34. gsmith Post author

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

  35. Jewel Walliser

    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!

  36. Brian M

    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?

Leave a Reply

Your email address will not be published. Required fields are marked *