Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to hide a navigation menu item under certain circumstances. Specifically, if a user is logged in AND has already registered their product bar code, I want to hide the 'Register Kit' link (which has a class of 'ac-regkit').

Below is the PHP I've added to the head section of header.php, just before wp_head();.

/* If User is loggied in, hide LOG IN and SIGN UP links */
<?php if ( is_user_logged_in() ) { ?>
    <style>
        .ac-login { display: none; }
        .ac-signup { display: none; }
    </style>

    /*If user has not yet registered their DNA Test Kit, show link */
    <?php
        $current_user = wp_get_current_user();
        $user_id = $current_user;
        $key = 'redeem_code';
        $single = true;
        $ac-barcode = wp_get_user_meta( $user_id, $key, $single);
        if ( $ac-barcode = '' ) { ?>
        <style>
            .ac-regkit { display: inline; font-weight: bold; }
            .ac-regkit a { color: red; }
        </style>
    <?php }; ?>
<?php } ?>

And this is the error I'm getting:

Server Error The website encountered an error while retrieving http://athleticode.com/. It may be down for maintenance or configured incorrectly.

I'm not an expert in PHP, so I'm sure it's a syntax error or something like that -- and there is probably a better way to achieve this result -- so any help would be greatly appreciated.

share|improve this question
    
It should be noted that the default CSS for .ac-regkit is 'display: none;'...and there is some redundancy in the variables above. Changed to $user_id = wp_get_current_user();. –  Shaun Scovil May 27 '11 at 16:45
    
A simple PHP error (syntax or runtime) should not result in a server error. Did you change anything else, for example in your .htaccess file? Enable WP_DEBUG to see detailed error messages. –  Jan Fabry May 27 '11 at 18:21
    
@Jan Fabry: If the php is correct, this defies logic...because everything works fine with just the is_user_logged_in section and not the get_user_meta part. There has to be something wrong with the code. –  Shaun Scovil May 28 '11 at 1:39

3 Answers 3

up vote 0 down vote accepted

I'd have 2 menus, and show the 2nd only if the user meet criteria. Then, use CSS to show both menus like its only one.

I think it's the easiest way. And you have the benefit that the items aren't really there, not just hidden.

share|improve this answer
    
@Daniel Dvorkin: Thanks for the response. That's a good suggestion, but even in that event I need to have a PHP conditional statement determining whether or not the current user has already registered their kit. Any idea why that isn't working, based on the code above? –  Shaun Scovil May 27 '11 at 17:32
    
Yes, the function name wp_get_user_meta is incorrect. Try get_user_meta codex.wordpress.org/Function_Reference/get_user_meta –  MZAweb May 27 '11 at 19:13
    
and you should have define('WP_DEBUG', true); in wp-config while developing in order to see details of your errors –  MZAweb May 27 '11 at 19:16
    
@Daniel Dvorkin: Thanks again, but I've tried 'get_user_meta' as well...no dice. The site is live, with customer traffic coming in due to the client's marketing campaign, so I'll have to wait until the wee hours to debug it I guess. –  Shaun Scovil May 28 '11 at 1:35
    
Try to get a comprensive detail of the error. Maybe a error.log in your site root? Better yet, try to get a local copy of the site in your machine to dev / debug. –  MZAweb May 28 '11 at 3:57

wp_get_current_user() returns the complete user object, so your next line should not be $user_id = $current_user, but $user_id = $current_user->ID.

I understand you can't enable WP_DEBUG on a live site, and this might be the reason you get a "generic" error message when something goes wrong, but have you looked at the options to write PHP error messages to a log file instead? This way the user won't see the errors but you can. Developing without seeing error messages is almost impossible.

share|improve this answer

In case this was your real code and not just an example, replace it with the following one. Btw: <!-- this is a html comment --> and <?php /* this is */ # like this or // that a php comment ?>.

<?php 
// Note: I moved all php-Comments inside the php tags. 
/* If User is loggied in, hide LOG IN and SIGN UP links */
if ( is_user_logged_in() ) 
{ 
?>
    <style>
        .ac-login { display: none; }
        .ac-signup { display: none; }
    </style>

    <?php
    /* If user has not yet registered their DNA Test Kit, show link */
        $current_user = get_currentuserinfo(); // THIS IS THE FUNCTION YOU NEED
        $user_id = $current_user->ID;
        $key = 'redeem_code';
        $single = true;
        $ac_barcode = wp_get_user_meta( $user_id, $key, $single);
        if ( empty( $ac_barcode ) ) { // YOU COULD TRY if ( ! $ac-barcode ) TOO
?>
        <style>
            .ac-regkit { display: inline; font-weight: bold; }
            .ac-regkit a { color: red; }
        </style>
<?php 
    } // removed ";"
}?>
share|improve this answer
    
This was very helpful, thanks! Unfortunately, I'm still getting the error. Everything works fine when I just have this: <?php if ( is_user_logged_in() ) { ?> <style> .ac-login { display: none; } .ac-signup { display: none; } .ac-regkit { display: inline; font-weight: bold; } .ac-regkit a { color: red; } </style> <?php } ?> ...but that doesn't hide the Register link. –  Shaun Scovil May 27 '11 at 19:49
    
Take Firebug AddOn for FireFox and search for an ID or class to hide it. –  kaiser May 27 '11 at 20:04
    
I've created the class (.ac-regkit)...but I'm trying to hide it only if the current logged in user has a non-empty value in their 'redeem_code' usermeta field. I'm just not writing the conditional PHP statement correctly and can't figure it out... :-/ –  Shaun Scovil May 28 '11 at 1:31
    
a) See Edits and comments b) You must not use "-" in php variables - use "_" instead. –  kaiser May 28 '11 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.