我正在使用一个代码段在我的网站导航菜单中添加一个登录/注销按钮。我想将登录链接更改为我的自定义页面“ / login”
我添加了最后一部分,以便注销将重定向到主页。我不知道要替换什么来更改登录链接。
add_filter('loginout', 'loginout_selector');
function loginout_selector($text) {
$selector = 'class="logout-link" style="font-size: 0.85em, text-align: center;"';
$text = str_replace('<a ', '<a '.$selector, $text);
$text = str_replace("Log out", "Logout", $text);
return $text;
}
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li style="list-style: none; text-align: center;">'. $loginoutlink .'</li>';
return $items;
}
//redirect to homepage after logout.
add_action('wp_logout','unlog');
function unlog(){
wp_redirect( site_url() );
exit();
}
答案 0 :(得分:1)
wp_loginout('index.php')将重定向到默认的wordpress登录页面。如果创建自定义登录页面,则自定义wp_loginout函数。
你可以喜欢。
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
/************************************************************************
1- If user is not login, When click on login button will redirect to site_url()/custom_login_page
2- With button you can add your custom class
*****************************************************************************/
if ( ! is_user_logged_in() ) {
$link = '<a href="' . site_url() . '/login">' . __( 'Log in' ) . '</a>';
}else {
$link = '<a href="' . esc_url( wp_logout_url( $redirect ) ) . '">' . __( 'Log out' ) . '</a>';
}
$items .= '<li style="list-style: none; text-align: center;">'. $link .'</li>';
return $items;
}