duan4739 2019-07-09 14:15
浏览 66
已采纳

用自定义登录页面替换wp登录

I'm using a snippet to add a login/logout button to my sites nav menu. I want to change the login link to my custom page "/login"

I added the last section so that logging out will redirect to the home page. I don't know what to replace to change the login link.

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();
}
  • 写回答

1条回答 默认 最新

  • duanqian6295 2019-07-09 15:09
    关注

    wp_loginout('index.php') will redirect to default wordpress login page. if you create the custom login page customize the wp_loginout function.

    you can do like.

    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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办