I've just finished to make some fixes on a WordPress site which makes use of the Quote Cart plugin. Apparently, one must be a registered user to properly manage orders with this plugin. So they asked me to add a login/logout link, a register link and a user redirect action to the web site. Let's see the code.
To add the login/logout links to the WordPress menu, simply use the following code in your functions.php file:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
$loginoutlink = wp_loginout('index.php', false);
$registerlink = wp_register('', '', false);
$items .= '<li class="menu-item login">'. $loginoutlink .'</li>' . '<li class="menu-item register">' . $registerlink . '</li>';
return $items;
}
To redirect a user after being logged in, use the following code:
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
function my_login_redirect( $redirect_to, $request, $user ) {
if ( is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
return home_url( '/wp-admin/' );
} else {
return home_url();
}
}
}
As you can see, I've used two simple filter functions to accomplish these tasks.