scriptygoddess

04 Oct, 2002

Show Hide (sidebar) gadgets

Posted by: Jennifer In: Scripts

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

October 3rd, 2002 at 3:41 pm

Avatar

Here is a zip with the code

There's no link to the download! :(

2 | Jennifer

October 3rd, 2002 at 3:49 pm

Avatar

DOH! Sorry about that. All fixed :)

3 | ste

October 3rd, 2002 at 5:51 pm

Avatar

OOOOOoooooh, neat demo. :)

4 | Office Park Dad

October 3rd, 2002 at 6:53 pm

Avatar

Way cool goddessJenn!

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

5 | Jason

October 4th, 2002 at 11:37 am

Avatar

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

October 4th, 2002 at 12:27 pm

Avatar

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

October 4th, 2002 at 11:15 pm

Avatar

Jason – NEAT! I like that idea. :)

8 | mike

October 5th, 2002 at 3:36 am

Avatar

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

October 5th, 2002 at 7:40 am

Avatar

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

10 | Mariann

October 5th, 2002 at 7:48 am

Avatar

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

October 5th, 2002 at 8:07 am

Avatar

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 | Sasha

October 14th, 2002 at 10:29 am

Avatar

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.

13 | An admirer

November 2nd, 2002 at 5:02 pm

Avatar

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

Thanks for this great script!

14 | Jennifer

November 2nd, 2002 at 5:11 pm

Avatar

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

15 | Anonymous

November 2nd, 2002 at 6:41 pm

Avatar

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

16 | monica

December 1st, 2002 at 2:14 pm

Avatar

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

17 | monica

December 2nd, 2002 at 11:32 am

Avatar

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.

18 | Jennifer

December 2nd, 2002 at 12:14 pm

Avatar

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

19 | brad

February 26th, 2003 at 9:54 am

Avatar

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.

20 | Jennifer

February 26th, 2003 at 10:24 am

Avatar

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… :-\

21 | James

May 20th, 2003 at 4:09 pm

Avatar

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

22 | Jennifer

May 21st, 2003 at 9:45 pm

Avatar

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>

23 | james

June 1st, 2003 at 1:34 pm

Avatar

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);
}
}
}

24 | Live in the Delirious Cool

October 8th, 2002 at 8:58 am

Avatar

Sidebar mania!
So, tell me about your perfect sidebar – lots of stuff, a little? Do you use my sidebar or your

25 | Al-Muhajabah's Islamic Blogs

February 5th, 2003 at 5:48 am

Avatar

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

26 | fuddland

February 22nd, 2003 at 9:42 pm

Avatar

viewer's choice
inspired by this but put off by the complicatedness* of the php, i ventured out into the big scary world

27 | fuddland

February 22nd, 2003 at 9:57 pm

Avatar

viewer's choice
inspired by this but put off by the complicatedness* of the php, i ventured out into the big scary world

28 | Al-Muhajabah's Movable Type Tips

March 7th, 2003 at 8:34 pm

Avatar

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…

29 | Dan Dickinson: The Primary Vivid Weblog

May 7th, 2003 at 10:39 pm

Avatar

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…

30 | Sideblog MoK

September 2nd, 2003 at 4:27 pm

Avatar

Show/Hide sidebar gadgets
show/hide sidebar gadgets…

31 | Rizwan Kassim's Public Log

November 5th, 2003 at 6:28 pm

Avatar

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

August 15th, 2004 at 5:10 pm

Avatar

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…

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements