0

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!

2
  • you use a <div> inside a ul that is invalid HTML.
    – janw
    Sep 26, 2012 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, 2012 at 11:03

2 Answers 2

0

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

        <a href="<?php $redirect = bloginfo('url'); wp_logout( $redirect, $echo ); ?>">
            Logout
        </a>
0

Change this:

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

to this:

<?php wp_loginout( home_url( '/' ) ); ?>
6
  • 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, 2012 at 8:54
  • @Rob, I've changed the code above. Sorry, I've missed the point in the beginning.
    – Max Yudin
    Sep 26, 2012 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, 2012 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, 2012 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, 2012 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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