Apologies in advance: I'm trying to learn PHP on the fly in order to take care of a client's request, so I may not know the best way to phrase some of my questions. Basically I am working on a site that has already been built in Wordpress CMS by a third party that I cannot contact. Currently it is set up so that in the page.php file there is a certain selection of links that are visible on every page to any visitor (logged in or not), and there is a simple "if" statement that makes an additional set of links visible on all pages if the user is logged in.
However, the client essentially wants to change this and create 2 classes or types of users, each one seeing a different set of links in the sidebar when logged in.
Is there a way I can accomplish this using a similar "if" statement - this is where my lack of knowledge of terminology and applications of PHP comes in - in order to distinquish between two different types of user accounts? So far I only know that I can specify "is_user_logged_in", but perhaps there are other properties of "user" that I can use.
I'm hoping there's away I can create and assign a certain class to each user, and then display a different menu depending on what type of user is logged in.
I looked in the FAQs and at the first couple pages of posts and didn't see anything (although I'm not sure how the question would/should be phrased), so I hope this isn't a repost!
New to PHP: Using "if" statement for custom sidebar
Moderator: General Moderators
Re: New to PHP: Using "if" statement for custom sidebar
Well, you could add a extra mysql table/field to accomplish this.
Not familiar with that CMS, but you could add some extra lines to the MySQL query which is responsible for getting user data from the database.
For example:
Then use that value to accomplish what you need.
I hope it helps you.
Not familiar with that CMS, but you could add some extra lines to the MySQL query which is responsible for getting user data from the database.
For example:
Code: Select all
$usertype = $userdata['type'];
Code: Select all
<?php if ($usertype != "1"){ stuff not to do here }
or
if ($usertype == "1"){ stuff to do here }
?>
Re: New to PHP: Using "if" statement for custom sidebar
I'm not 100% sure about how Wordpress works, but I think this should work. The different roles are subscriber, contributor, author, editor, and administrator.
Code: Select all
if (get_role() == 'subscriber') {
...
}
else {
...
}
Re: New to PHP: Using "if" statement for custom sidebar
That actually might work, since those roles are already built in to the Wordpress system.MichaelR wrote:I'm not 100% sure about how Wordpress works, but I think this should work. The different roles are subscriber, contributor, author, editor, and administrator.
Code: Select all
if (get_role() == 'subscriber') { ... } else { ... }
Re: New to PHP: Using "if" statement for custom sidebar
Tried out that solution last week, and it didn't work - It looked like it would, but for whatever reason it just generated an error message on the page rather than calling up the menu options.
Asking some coder friends for advice, but if anyone else has any suggestions, let me know!
It doesn't even have to be based on the Wordpress user role, if you know another way to designate which menu is displayed to a certain type of user (maybe it can look for a certain word in the user name itself?). We're just looking for a way to have one of two different menus display depending on who is logged in.
Asking some coder friends for advice, but if anyone else has any suggestions, let me know!
It doesn't even have to be based on the Wordpress user role, if you know another way to designate which menu is displayed to a certain type of user (maybe it can look for a certain word in the user name itself?). We're just looking for a way to have one of two different menus display depending on who is logged in.
Re: New to PHP: Using "if" statement for custom sidebar
First of all you need to store the user role in a variable, let's say $userRole, which I will assume comes from $_POST. Since you have a few user roles I will use a switch.
Code: Select all
<?php
// define links by role:
$subscriberLinks = '<a href="link1.php"> Link1 </a> <a href="link2.php"> Link2 </a>';
$authorLinks = '<a href="link3.php"> Link3 </a> <a href="link4.php"> Link4 </a>';
$editorLinks = '<a href="link5.php"> Link5 </a> <a href="link6.php"> Link6 </a>';
$administratorLinks = '<a href="link7.php"> Link7 </a> <a href="link8.php"> Link8 </a>';
// The following controls the value of $userRole
$userRole = $_POST['role']; // or.. it could be $_GET['role']...
// I will force the role value just to make the code work:
$userRole = "author"; // delete later, I am a temporary line
switch ($userRole)
{
case 'subscriber': echo $subscriberLinks; break;
case 'author': echo $authorLinks; break;
case 'editor': echo $editorLinks; break;
case 'administrator': echo $administratorLinks; break;
default: break; // don't display anything if role is not identified
}
?>
Last edited by manohoo on Mon Dec 28, 2009 11:54 am, edited 2 times in total.
Re: New to PHP: Using "if" statement for custom sidebar
Thanks, I will try that out!