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:

<?php
global $userdata;
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!

You may also like...

5 Responses

  1. Mert Erkal says:

    Hi Sarah,

    I have recently discovered your blog. I was the one who mentioned your name in Wp Thoughts blog as a guest blogger. I think you are doing a great job. I will keep on watching you by subscribing to your RSS feeds.

    Cheers,
    Mert

  2. Sarah says:

    Hi Mert, cheers for stopping by and thanks for the mention in your post. Hope you find somethings of use here 🙂

  3. Bob! says:

    Superb Sarah, this is something I’ve thought about a few times, but never did get around to figuring out 🙂

    I can’t even remember what I wanted it for now, but still handy to know it’s here if I remember 😀

  4. Sarah says:

    Bob, it’s such a simple little bit of code. I only found it when someone once asked how they could hide their MyBlogLog from public view. It’s definitely handy though, especially for client sites so that they can have their login links on the front and the general public can’t see them.

  5. David says:

    Hi
    Great piece of code. I came across your site by accident. How lucky was that?
    Now have a link on my desktop. Great site keep up the good work.

    David

Leave a Reply

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