1

I'm developing a plugin, with a login system.

I need to conditionally remove a menu entry from a nav menu on the frontend, if $_SESSION['member-user'] is set.

For example, I want a 'register' link to only appear if $_SESSION['member-user'] is not set

I did try to search in codex without success. Thank you!

EDIT:

To explain, the menu is configured from admin of wordpress website. I need to delete the link by my plugin. The menu is not echoed from my plugin, but configured from admin (the admin will use my plugin).

Thank you

NEW EDIT:

The process of my plugin is the following:

In "readme" you will read:

1 - create a page with shortcode [login] inside and connect to a menu voice "User" > "Login" 2 - create a page with shortcode [balance] inside and connect to a menu voice "User > "Balance"

If user is logged (not a wordpress user, login is external), I need to remove that first page "Login" (or "Register", not very important in this case)...

I hope it is more clear now... thank you to all!

3
  • I'm not sure how this relates to WordPress, it looks like a standard PHP question for which an if statement with the isset or empty functions as the check would work. WordPress does't use sessions natively either
    – Tom J Nowell
    Apr 21, 2017 at 13:49
  • Ah! I've edited your question so that it's clearer, and taken it off of hold. Thanks for the clarification :)
    – Tom J Nowell
    Apr 21, 2017 at 15:59
  • Thank you @TomJNowell , but I did make another edit to better explain... thank you!
    – sineverba
    Apr 21, 2017 at 22:43

4 Answers 4

1

you can write like below for WordPress :

<?php
if ( !is_user_logged_in() ) { ?>
    <ul>
        <li><a href="login">Login</a></li>
        <li><a href="register">Register</a></li>
    </ul>
<?php } 
0
1

One way to do this is to have 2 menus, and show a different menu depending on wether the user is logged in or not, e.g.:

wp_nav_menu( array(
    'theme_location' => is_user_logged_in() ? 'logged-in-menu' : 'logged-out-menu'
) );

You can swap out the is_user_logged_in() call with a check of your own

0

You can just wrap the output in your plugin with a conditional like this:

<ul>
  <li><a href="login">Login</a></li>
  <?php if ( !$_SESSION['member-user'] ) : ?>
    <li><a href="register">Register</a></li>
  <?php endif; ?>
</ul>
0
0

As you are developing plugin. You can use following code to add menu links dynamically based on SESSION value

function new_nav_menu_items($items) {
if(isset($_SESSION['member-user'])){
    $loginlink = '
    <li class="home"><a href="' .get_page_link('YOUR_LOGIN_PAGE_ID_HERE').'?from='.$post_slug.'">' . __('Login') . '</a></li>
    <li class="home"><a href="' .get_page_link('YOUR_REGISTER_PAGE_ID_HERE').'?from='.$post_slug.'">' . __('Register') . '</a></li>
    ';
    $items = $items . $loginlink;
} else {
    $accountlink = '
    <li class="home"><a href="' .get_page_link('YOUR_MY_ACCOUNT_PAGE_ID_HERE').'?from='.$post_slug.'">' . __('My Account') . '</a></li>
    <li class="home"><a href="' .get_page_link('YOUR_LOGOUT_LINK_HERE').'?from='.$post_slug.'">' . __('Logout') . '</a></li>
    ';
    $items = $items . $accountlink;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

Your Answer

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

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