Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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 ";"
}?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top