如何在WordPress插件中添加注销功能


How do I add a logout feature in a WordPress plugin?

我是WordPress的新手,正在开发一个可以进行登录、注销和注册的WP插件。到目前为止,这个插件可以登录,但我无法让它注销。我尝试过unset($_SESSION['user_id']);session_destroy();函数,但它们都不起作用。

下面是我可以登录的代码,有人能帮我做注销功能吗?


<?php
    /*this block of code is preventing user from submitting empty email or password*/
    if(isset($_POST['email']) && isset($_POST['password'])){
        $email = $_POST['email'];
        $password = $_POST['password'];
        $pass_hash = md5($password);
        if( !empty($email) && !empty($password) ){
            $query="SELECT `ID` FROM `vrm_users` WHERE `user_email`='$email' AND `user_pass`='$pass_hash'"; 
            if($query_run= mysql_query($query)){
                $query_num_rows= mysql_num_rows($query_run);
                if($query_num_rows==0){
                    echo'Invalid Login';
                }else if($query_num_rows==1) {
                    $user_id=mysql_result($query_run, 0, 'ID');
                    $_SESSION['user_id'] = $user_id;
                    //header('Location: test_form.php');
                }//if($query_num_rows==0) ends here

            }//if($query_run= mysql_query($query)) ends here    
        }else{
            echo'Please Enter Email & Password';
        }//if( !empty($email) && !empty($password) ends here!

    }//if(isset($_POST['email']) && isset($_POST['password']) ends here


    /*
    Shortcode: [loginform]
    Description: Craetes A form that takes inputs
    */
    /*  Login Form  */
    function form_shortcode(){
        return  '<div class="loginform">
            <h2>Custom VRM LOGIN FORM</h2>
            <form action="" method="POST">
                <input type="email" name="email">Email<br>
                <input type="password" name="password">Password<br>
                <input type="submit" name="submit" value="Login"><br>
            </form>
            </div>';
    }//form_shortcode()

    add_shortcode('loginform', 'form_shortcode');


?>

您可以使用wp_logout_url()模板标记在插件文件中生成注销链接。

<a href="<?php echo wp_logout_url( home_url( '/redirect_page' ) ); ?>" title="Logout">Logout</a>

看看wp_logout_url()

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