User Content Control in WordPress
At times you may find that you want to have a few extra gimmicks (links, widgets etc) displayed to you on your WordPress site however you don’t want visitors to see these. Perhaps it may be an admin link, to save you having to type it in, or maybe a widget of some kind. This is easily done with a little bit of code as shown below:
if ($userdata->ID > 0) :
Put your output here
<?php endif; ?>
The first line of this code makes the userdata variable global so that we can access information from it. Then we check the ID of the current user. I’ve set it so that it checks to see if the ID is greater than zero, ie. it’s a logged in user. This would mean anyone who’s logged in would see the content you output. Then, where it says ‘Put your output here’, is where (obviously) you put your output! This can be anything you want. I’ve used it in the past for quick admin links through to wp-admin, testing additional sidebar content so that it doesn’t disrupt the actual site, plus I have a couple of widgets display on the left side of the screen which just gives me a bit more info but stuff I don’t want to necessarily have public.
If you have other users and you don’t want them to see what you can see, then you can change the if statement to check that the user ID is your admin ID. This is usually 1, however you may need to check on this. This is easily done by going to Authors and Users and looking at the ID number next to your name in the list of users. Once you have this ID then simply change the if statement to:
if ($userdata->ID == X) :
Where X is your ID. Note the two equals signs. Make this is 2 and not just 1 else everyone will see the hidden output and you could also cause other problems on your site.
Then finally you close the if statement when you’ve finished the output you want kept hidden from the public. This doesn’t have to just be used for a one liner, it can be used to output anything you want from one line to many. You can even use an if/else statement so that you display one thing to yourself and something different to everyone else. Of course you don’t want to go overboard with this else you would have to always to ensure you checked your site without being logged in, just to check that nothing’s gone wrong 😉
If you want to repeat this within the header, content, sidebar or footer, then you only need the global $userdata line once before the first time you use the if statement.
PS. Yes I didn’t know what to call this post which is why it’s a bit naff!