从wordpress中的登录表验证用户并显示注销按钮代替登录按钮?

时间:2014-01-23 07:48:32

标签: php wordpress login

我创建了一个模板,我通过该模板运行用户身份验证查询。问题是用户重定向到他/她的个人资料登录按钮后仍然出现在菜单上。当用户登录时我想要登出按钮代替登录按钮,登出后它重定向到我的网站的主页而不是wordpress仪表板。这是代码:

<?php
/**
 * Template Name: user-login
 *
 * Description: Twenty Twelve loves the no-sidebar look as much as
 * you do. Use this page template to remove the sidebar from any page.
 *
 * Tip: to remove the sidebar from all posts and pages simply remove
 * any active widgets from the Main Sidebar area, and the sidebar will
 * disappear everywhere.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */
get_header(); ?>

<html>
    <body>
        <form method="post">
            <table align="center" cellspacing="10px">
            <tr>
                <td><font size="+1">Username:</font></td>
                <td><input type="text" name="uname1"></td>
            </tr>
            <tr>
                <td><font size="+1">Password:</font></td>
                <td><input type="password" name="upass1"></td>
            </tr>
            <tr>
                <td><input type="submit" name="login" value="login"></td>
            </tr>
        </table>

        <?php
            if(isset($_POST['login'])) {
                global $wpdb;
                $name=$_POST['uname1'];
                $pass=$_POST['upass1'];

                $sql="select * from wp_member where username='".$name."' and     password='".$pass."'";
                $row=$wpdb->get_row($sql);
                $id=$row->id;
                if($row>0) {
                    echo "<script>window.location.href='http://localhost/wordpress?page_id=387&    id=$id'</script>";
                    echo '<a href="'.wp_logout_url( get_permalink() ).'" title="Logout"      class="hunderline">Logout</a>';   
                } else {
                    echo "<div style='color: red'>Invalid User</div>";
                }
            }
        ?>
    </body>

<?php
    /*if ( is_user_logged_in() ) { } else {
     echo '<a href="'.wp_login_url( get_permalink() ).'" title="Login" class="hunderline">Login</a>';
    }*/
    get_footer();
?>

1 个答案:

答案 0 :(得分:0)

您可以使用Wordpress条件标记来验证:

http://codex.wordpress.org/Function_Reference/is_user_logged_in

<?php
if ( is_user_logged_in() ) {
    //display the logout button
    <a href="<?php echo wp_logout_url(home_url()); ?>" title="Logout">Logout</a>
} else {
    //display login from
}
?>