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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号