Modify Header Links Plugin - Filter By User

By Rusty - January 6, 2011

Well here goes, did some searching, and couldn't come up with anything.

I have successfully installed the modfiyHeaderLinks.php plugin.

My goal is to filter what links are shown in the left header based upon user privileges level.

If the user is an Admin I want to show some links (Like to Google Analytics etc etc).
Else if the user is logged in I want to show some other links.
and setup like that it should ONLY display header links if there is a user that is an Admin, or if a user is logged in, and nothing if there is no user logged in.

This is some of the code (which doesn't work)... I've had limited success (none)

// add links at the end
$postLinks = '';

if (@$CURRENT_USER['isAdmin'] == 'true') {
$postLinks .= "<a href='https://www.google.com/analytics/settings/'>Google Analytics</a>";}
elseif (@$CURRENT_USER) {
$postLinks .= "<a href='https://www.domain.com/orderhistory/'>Order History</a>";}


Where am I going wrong. I'm trying to check/compare if the $CURRENT_USER isAdmin value is set to true then it'll show the admin links.
Rusty

Re: [Rusty] Modify Header Links Plugin - Filter By User

By Jason - January 6, 2011

Hi Rusty,

What is the result of your code? Are you getting an error? Does it seem to do anything at all?

You can try changing your first if statement like this:

if (@$CURRENT_USER['isAdmin']) {

If you're still experiencing the problem, please attach the .php file so I can take a look at everything you've got and let me know exactly what happens when you run the code.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Modify Header Links Plugin - Filter By User

By Rusty - January 6, 2011

Hi Rusty,

What is the result of your code? Are you getting an error? Does it seem to do anything at all?

You can try changing your first if statement like this:

if (@$CURRENT_USER['isAdmin']) {

If you're still experiencing the problem, please attach the .php file so I can take a look at everything you've got and let me know exactly what happens when you run the code.

Hope this helps.


I'll give that a try my previous code, resulted in no Pre or Post header links showing at all.

I did as suggested, and I get no header links whether logged in as admin or regular user with the following code
// NOTE: If you upgrade this plugin be sure to BACKUP your changes so they don't get overwritten!

addFilter('header_links', 'modifyHeaderLinks');

//
function modifyHeaderLinks($html) {

// remove license link
$html = preg_replace("/<a href='\?menu=license'>.*?<\/a> \| /", "", $html);

// add links at the beginning
$preLinks = '';
// $preLinks .= "<a href='http://www.google.com/'>Google</a><br/>";

// add links at the end
$postLinks = '';

if (@$CURRENT_USER['isAdmin']) {
$postLinks .= "<a href='https://www.google.com/analytics/settings/'>Google Analytics</a>";
}
elseif (@$CURRENT_USER) {
$postLinks .= "<a href='https://www.google.com/analytics/settings/'>History</a>";
}


//
$html = $preLinks . $html . $postLinks;
return $html;
}

Rusty

Re: [Rusty] Modify Header Links Plugin - Filter By User

By Chris - January 6, 2011

Hi Rusty,

Adding this line as the first line of your function will fix it:

function modifyHeaderLinks($html) {
global $CURRENT_USER;


You don't need to do this outside of functions. I forget to declare globals in my functions all the time. :)

Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Modify Header Links Plugin - Filter By User

By Rusty - January 6, 2011 - edited: January 6, 2011

Hi Rusty,

Adding this line as the first line of your function will fix it:

function modifyHeaderLinks($html) {
global $CURRENT_USER;


You don't need to do this outside of functions. I forget to declare globals in my functions all the time. :)

Please let me know if you have any questions.


D'oh!

Thanks Chris. At least I managed to stop losing my keys by putting them on a huge bright blue lanyard...

BTW. I vote that we add this as an update to the modifyHeaderLinks.php plugin for the next release.

With a bit more customization I think we could have:
Global Links
Admin Only Links
Editor Only Links
User Only Links
and any wonderful combo we'd like to mix-match & mash together.

And for the record, though sometimes I get confused about how I should order my IF ELSE statements it absolutely love them. So easy to dynamically present and format content.

*cue Captain Planet theme*
With the powers of PHP/CMSB & CSS Combined... I'm gonna get back to work now.

Thanks as always. You guys really do have the best and most rapid support I've ever come across. Don't Stop!
Rusty