Strings Strings - 7 months ago 52
PHP Question

WordPress login & logout with UserPro in menu

I'm using a plugin called "UserPro". I'm also using a function in my menu to show either "Login" or if logged in "Logout".

function autov_add_loginout_navitem($items) {
$login_item = '<li class="login">'.wp_loginout($_SERVER['REQUEST_URI'], false).'</li>';
$items .= $login_item;
return $items;
}
add_filter('wp_nav_menu_account_items', 'autov_add_loginout_navitem');


the plugin has a short code

<?php echo do_shortcode('[userpro template=login]'); ?>


How can I implement this in the first code?

(This was placed inside functions.php in my child-theme)

Answer

That shortcode would need to in a page. Create a page and add the shortcode. From there you can link to the page for login. The logout link will remain the same.

function autov_add_loginout_navitem($items) {
    $format = '<a href="%s">%s</a>';
    $link = is_user_logged_in()
        ? sprintf($format, wp_logout_url(), 'Logout')
        : sprintf($format, home_url('login'), 'Login');
    $login_item = '<li class="login">'.$link.'</li>';
    $items .= $login_item;
    return $items;
}
add_filter('wp_nav_menu_account_items', 'autov_add_loginout_navitem');

The above example assumes you have created a page with the slug login and added your shortcode there. Note that the wp_logout_url function can take a redirect link as a parameter, you should specify that parameter if you want to send a user somewhere after logging out.

Comments