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 want to display a different menu depending whether a user is logged into the CMS or not.

I'm using this for my menu:

<?php if ( is_user_logged_in() ) {
$args = array(
    'depth'        => 1,
    'show_date'    => '',
    'date_format'  => get_option('date_format'),
    'child_of'     => 0,
    'exclude'      => '5, 80, 83, 138', 
    'include'      => '',
    'title_li'     => __(''),
    'echo'         => 1,
    'authors'      => '',
    'sort_column'  => 'menu_order, post_title',
    'link_before'  => '',
    'link_after'   => '',
    'walker' => '' );
?>
<ul id="menu-list"> 
    <div class="menu-button">
        <?php wp_list_pages( $args, 'sort_column=menu_order' ); ?>
        <li class="page_item page-item-99">
            <a href="<?php $redirect = bloginfo('url'); wp_logout_url( $redirect ); ?>">
                Logout
            </a>
        </li>
    </div> 
</ul>
<div class="divider"></div>
    <p id="board">Viewing content for: <br />Aneurin Bevan Health Board</p>
<div style="margin-top:7px!IMPORTANT;" class="divider"></div>
<?php } else {
$args1 = array(
    'depth'        => 1,
    'show_date'    => '',
    'date_format'  => get_option('date_format'),
    'child_of'     => 0,
    'exclude'      => '25, 31, 33, 43, 93, 96, 99, 138',    
    'include'      => '',
    'title_li'     => __(''),
    'echo'         => 1,
    'authors'      => '',
    'sort_column'  => 'menu_order, post_title',
    'link_before'  => '',
    'link_after'   => '',
    'walker' => '' );
?>
<ul id="menu-list"> 
    <div class="menu-button">
        <?php wp_list_pages( $args1, 'sort_column=menu_order' ); ?>
    </div> 
</ul>
<?php } ?>

I'm using the following for a user to logout of the CMS:

<a href="<?php $redirect = bloginfo('url'); wp_logout_url( $redirect ); ?>">Logout</a>

Even though the page reloads after clicking the logout link, the menu doesn't seem to change. Does anyone know what's going wrong?


UPDATE

I've tried using <?php $redirect = bloginfo('url'); wp_logout( $redirect, $echo ); ?> which I thought was working perfectly until a colleague shouted across the room that they had been logged out too! Turns out it's logging everyone out instead of just logging out the user that clicked the logout link!

share|improve this question
    
you use a <div> inside a ul that is invalid HTML. –  janw Sep 26 '12 at 8:34
    
After update: It's impossible to force somebody to logout because logout happens in current user browser only (by deleting browser cookies). Session information is not stored somewhere else, e.g. database. –  Max Yudin Sep 26 '12 at 11:03
add comment

2 Answers 2

up vote 0 down vote accepted

Seems as though the session needed to be destroyed with the following:

        <a href="<?php $redirect = bloginfo('url'); wp_logout( $redirect, $echo ); ?>">
            Logout
        </a>
share|improve this answer
add comment

Change this:

<a href="<?php $redirect = bloginfo('url'); wp_logout_url( $redirect ); ?>">Logout</a>

to this:

<?php wp_loginout( home_url( '/' ) ); ?>
share|improve this answer
    
I have two completely different menus with that wrapped around the top and an else statement but that doesn't seem to work. –  Rob Sep 26 '12 at 8:54
    
@Rob, I've changed the code above. Sorry, I've missed the point in the beginning. –  Max Yudin Sep 26 '12 at 9:26
    
I tried this - <?php $redirect = bloginfo('url'); wp_loginout( $redirect, $echo ); ?> but again the menu doesn't change. It definitely seems as though it's not logging me out fully. –  Rob Sep 26 '12 at 9:37
    
Have you tried my updated code? And also have a look into logout link url in both versions. It should contain .../wp-login.php?action=logout&redirect_to=... –  Max Yudin Sep 26 '12 at 9:46
    
Yep tried it but the link doesn't contain that extra stuff in the url, what am I missing? My answer using wp_logout destroys everyones session so everyone gets logged out when one person clicks the link! –  Rob Sep 26 '12 at 9:54
show 2 more comments

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.