Show Hide (sidebar) gadgets

UPDATE 10/5: I’m releasing the v2 on this. :)

Lest you thought we disappeared… I have a script for you! :)
This one is not an easy one to implment, but there’s lot of instructions to help you along the way. However, I STRONGLY suggest you have some knowledge of PHP before you attempt this one. The idea here is - lets say you have a lot of junk in your sidebar. Maybe some users are interested. Maybe not. With this script, your users can customize the page… showing/hiding whatever sidebar elements they want. All the info is stored in a single cookie.

(V2 lets you rearrange the order of the gadgets by moving one of them to the top. The one drawback here is that the way the script is written - you kind of have to have your “gadgets” all stacked together. You could, with some tweaking in v1- scatter them… so I’m leaving both up for downloading depending on what you want to do)

Here is the demo page (v1)
(fyi - that demo page looks like POO! especially on a Mac… but it’s just a proof of concept)
Here is a zip with the code (v1)

Here is the demo page (v2)
Here is a zip with the code (v2)

As I warned in the instructions: I make no guarantees. It’s free, okay? Use it. Edit it. I’d love a link back here. :) But unfortunately I have very little time for support. You’re always welcome to post a comment here to see if there’s someone out there that can help you. (*please note: if there are already comments here - PLEASE READ THEM before posting your comment - the problem may already have been addressed by someone else)

Enjoy!

(P.S. Thanks to Promoguy for his help/inspiration on this)

32 Responses to “Show Hide (sidebar) gadgets”

  1. Mariann Says:

    Here is a zip with the code

    There’s no link to the download! :(

  2. Jennifer Says:

    DOH! Sorry about that. All fixed :)

  3. ste Says:

    OOOOOoooooh, neat demo. :)

  4. Office Park Dad Says:

    Way cool goddessJenn!

    Nice way to make a lot of info available on the index page.

  5. Jason Says:

    Instead of using it for sidebar content, you could also use this for showing/hiding the ‘more’ and/or ‘excerpt’ of an entry. For an example of what I mean, check out my demo here.

  6. Promo Says:

    I have this working on my site, if you would like to see it in action. One on the left sidebar, one on the right. Hope to add more soon.

    Thanks Jenn!

  7. Jennifer Says:

    Jason - NEAT! I like that idea. :)

  8. mike Says:

    i’ve never visited your site before, but i have to tell you that if i based my opinion on this script alone - nice effing work.

    thanks

  9. Donna Says:

    Oooh, just what I need to get “sidebar mania” under control.

  10. Mariann Says:

    That is really cool — does it *work* on a Mac or does it just look icky? Whenever I design, I have to consider my husband because he’s a Mac user, and I’ll be switching in the next six months so I try to keep these things in my head — and saved for future reference in text! ;)

  11. Jennifer Says:

    Yes - it works on a mac… The problem with the demo is that I just threw it together to show how it works.

    All the code is *server side* so it will work on ANY platform.

  12. Live in the Delirious Cool Says:
    Sidebar mania!
    So, tell me about your perfect sidebar - lots of stuff, a little? Do you use my sidebar or your

  13. Sasha Says:

    Heya, totally love this script! It’s something I’ve been looking for for ages! I was just wondering, did anyone here try to install this with skins (I have mine working with Amy’s tutorial over at Domesticat.net). If so, does anyone have any pointers for me? I’d love to have both working at the same time, but I’m a little unsure of how to go about it.

  14. An admirer Says:

    Is it possible to move a gadget up by one block instead of all the way to the top?

    Thanks for this great script!

  15. Jennifer Says:

    When coding, it was simpler to move it up to the top… I probably won’t have time to come out with a “version 2″ of it, but I’m sure it could conceivably be done. I’m sure it’s not that difficult - it just wasn’t worth it to me to do it…

    You’re alway free to hack the code and see if you can figure it out :) (If you do, I’d love it if you posted the link in the comments…)

  16. Anonymous Says:

    Here’s what I came up with. I’m a newbie to PHP so please tell me if I made any mistakes or if there’s an easier way to do it.

    Instead of the following code:
    $strToShow = $thisGadget;
    for ($x=0; $x

  17. monica Says:

    That’s wonderful! yay, thanks, Jenn you’re a star! exactly what I was looking for :-)

  18. monica Says:

    Something to add here - I was having problems with refreshing the page because my “gadgets” are all PHP includes - so basically, the show/hide *was* working (the hide and show links were alternating correctly) but you needed to refresh the page to actually see the content of the different gadget includes, and that meant going to the location bar and stripping away the setcookie?etc string to only have index.php (normal browser refresh would refresh the whole url + setcookie? string).

    So, I just checked around a bit on the php manual pages and other scripts and found that by adding all this stuff:

    header(”Expires: Mon, 26 Jul 1997 05:00:00 GMT”); // Date in the past
    header(”Last-Modified: ” . gmdate(”D, d M Y H:i:s”) . ” GMT”); // always modified
    header(”Cache-Control: no-store, no-cache, must-revalidate”); // HTTP/1.1
    header(”Cache-Control: post-check=0, pre-check=0″, false);
    header(”Pragma: no-cache”); // HTTP/1.0
    session_cache_limiter(’private_no_expire’);
    session_cache_limiter(’nocache’);
    header(”Location: $HTTP_REFERER”);
    exit;

    after the last line in the script, (ie. after the last setcookie(etc.) line), the page refreshes correctly by going to the url (index.php or anything else) rather than the url plus query string.

    Now I’m a php newbie so I have no idea if all those headers are necessary but it was only after adding them that I could get the whole thing to work without having to reload the page manually.

  19. Jennifer Says:

    Great job Monica! When I get a moment, I’ll add that into the instructions on the download…Thanks for sharing that. :)

  20. Al-Muhajabah's Islamic Blogs Says:
    finally, a solution to my sidebar problem
    My problem being that I can’t stop adding sidebar stuff ;) When you look at the sidebar items now, you’ll

  21. fuddland Says:
    viewer’s choice
    inspired by this but put off by the complicatedness* of the php, i ventured out into the big scary world

  22. fuddland Says:
    viewer’s choice
    inspired by this but put off by the complicatedness* of the php, i ventured out into the big scary world

  23. brad Says:

    anyone having trouble getting new gadgets to appear after the user has visted the site once already? even after refresh they are not appearing. the new gadget only appears after deleting the local cookie.

  24. Jennifer Says:

    Yeah, if you ADD new gadgets, the current script won’t automatically show it for repeat visitors. Since they already have a cookie, it doesn’t know to look for the new setup…

    There’s a workaround, that will check the length of the gadgets cookie, and if they have an older version, it will tack on the new gadgets at the bottom…

    (fyi - the max. number of gadgets you can ever have is nine… I know, it’s a design flaw…)

    In any case, lets say you used to have *6* gadgets and now you have *9*…so you’d now have:

    $numOfGadgets = 9;

    And here’s an additional block of code to add below that declaration…

    if (isset($_COOKIE['gadgets']) && strlen($_COOKIE['gadgets']) < $numOfGadgets) {
    $newCookieString = $_COOKIE['gadgets'].”111″;
    $_COOKIE['gadgets'] = $_COOKIE['gadgets'].”111″;
    setcookie(”gadgets”,”$newCookieString”,time()+126144000,”/”,”.YOURDOMAIN.COM”,0);
    }

    Obviously change YOURDOMAIN.COM and note the THREE “1″s - because we’re adding THREE new gadgets.

    THEN, your three new gadgets will be added to the bottom of your list as a default, here’s the code that will do that:

    if (isset($_COOKIE['gadgetOrder']) && strlen($_COOKIE['gadgetOrder']) < $numOfGadgets) {
    $_COOKIE['gadgetOrder'] = $_COOKIE['gadgetOrder'].”789″;
    }

    You see the “789″ part… that’s because we already have 1-6 for the first 6 gadgets. Gadget 7, 8, and 9, will be tacked on at the end… I guess technically you could move it to the front, if you wanted to, but this way it doesn’t upset the current order of your users gadgets…(They can easily move them to the top if they want to)…

    I’m not the best at instructions… and especially with this script being as crazy as it is… I hope that makes sense… :-\

  25. Al-Muhajabah's Movable Type Tips Says:
    show/hide sidebar blocks
    One of the tricks I’ve set up on the veiled4allah index page is to allow visitors to show or hide the sidebar blocks. I can’t stop adding sidebar stuff, but not everybody wants to look at all of it. Now…

  26. Dan Dickinson: The Primary Vivid Weblog Says:
    Customizable Sidebar
    Behold, the answers to your plagued problems with my sidebar being too long are gone! Now you can click HIDE to get rid of any section you don’t care about. And, should you change your mind later, you can always click SHOW. All via the magic of cookies…

  27. James Says:

    How did you do the “Restore sidebar boxes to their defaults” ? Is that extra code? Thanks Jennifer!

  28. Jennifer Says:

    In the download there’s a section that looks like this (with comments removed):

    if (!isset($_COOKIE['gadgets'])) {
    setcookie(”gadgets”,”111″,time()+126144000,”/”,”.YOURDOMAIN.COM”,0);
    $gadgetstemp = “111″;
    }

    You can change it to this:

    if (!isset($_COOKIE['gadgets']) || isset($getdefaultgadgets)) {
    setcookie(”gadgets”,”111″,time()+126144000,”/”,”.YOURDOMAIN.COM”,0);
    $gadgetstemp = “111″;
    }

    or this if you added gadgets and making sure they’re seeing all the gadgets including the new one (see comments above for what I’m talking about):

    if (!isset($_COOKIE['gadgets']) || isset($getdefaultgadgets) || (strlen($_COOKIE['gadgets']) < $numOfGadgets) ) {
    setcookie(”gadgets”,”111″,time()+126144000,”/”,”.YOURDOMAIN.COM”,0);
    $gadgetstemp = “111″;
    }

    Then on your page a simple link:

    <a href=”<? echo $PHP_SELF; ?>?getdefaultgadgets=1″>Restore sidebar boxes to their defaults</a>

  29. james Says:

    For this code that a visitor posted avobe for moving the boxes up by one instead of all the way to the top, there are some errors on it and it won’t workf or me. Any ideas anyone? Please?

    $pos = strpos($gadgetordertemp, “$thisGadget”);
    if ($pos != 0) {
    for ($x=0; $x if ($x == $pos-1) {
    $strToShow .= substr($gadgetordertemp, $x+1, 1);
    $strToShow .= substr($gadgetordertemp, $x, 1);
    $strToShow .= substr($gadgetordertemp, $x+2);
    break;
    } else {
    $strToShow .= substr($gadgetordertemp, $x, 1);
    }
    }
    }

  30. Sideblog MoK Says:
    Show/Hide sidebar gadgets
    show/hide sidebar gadgets…

  31. Rizwan Kassim's Public Log Says:
    Look Mom!
    See what I’ve done? The side bars (just left for now) are collapsable, and not in the stupid way that the blogrolling is collapsable… It actually doesn’t transmit the data if it doesn’t need to, meaning an even quicker download…

  32. green gabbro Says:
    Sidebar Gadget Manager: Implementation Notes
    I’m currently installing the show/hide sidebar gadgets script, from scriptygoddess. Because I don’t want to have the script included on…