将wordpress注册嵌入主页

时间:2012-03-20 14:11:28

标签: ajax forms wordpress

我正在尝试将wordpress注册嵌入主页。我已经创建了一个自定义注册页面,问题是它调用了头信息,当我用php include包含它时,我得到了这个错误。

警告:无法修改标头信息 - 已发送的标头

有没有办法可以使用我的代码,但以某种方式改变它/以某种方式使用它,以便注册表单完全嵌入/使用ajax在页面中工作,但它的工作原理。我对Php并不陌生,所以请原谅这是一个愚蠢的问题:

当前注册码(在其自己的页面中工作,但不包括在主页模板中)

<?php
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
//Check whether the user is already logged in
if (!$user_ID) {

        if($_POST){
            //We shall SQL escape all inputs
            $username = $wpdb->escape($_POST['username']);
            if(empty($username)) { 
                echo "User name should not be empty.";
                exit();
            }
            $email = $wpdb->escape($_POST['email']);
            if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) { 
                echo "Please enter a valid email.";
                exit();
            }       

                $random_password = wp_generate_password( 12, false );
                $status = wp_create_user( $username, $random_password, $email );
                if ( is_wp_error($status) ) 
                    echo "Username already exists. Please try another one.";
                else {
                    $from = get_option('admin_email');
                    $headers = 'From: '.$from . "\r\n";
                    $subject = "Registration successful";
                    $msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";
                    wp_mail( $email, $subject, $msg, $headers );

                    echo "Please check your email for login details.";
                }

            exit();

        } else { 
            echo "";        
            ?>

            <!-- <script src="http://code.jquery.com/jquery-1.4.4.js"></script> --> <!-- Remove the comments if you are not using jQuery already in your theme -->


            <?php                   
            if(get_option('users_can_register')) { //Check whether user registration is enabled by the administrator
            ?>

            <h1><?php the_title(); ?></h1>
            <br /><br />
            <div id="result"></div> <!-- To hold validation results -->
            <form id="wp_signup_form" action="" method="post">

            <label><p>Username:</p></label>
            <input type="text" name="username" class="text" value="" />
            <br /><br />
            <label><p>Email address:</p></label>
            <input type="text" name="email" class="text" value="" /> <br />
            <br />
            <input type="submit" id="submitbtn" class="Buttons" name="submit" value="Register" />
            <br />
            <br />
            </form>

            <script type="text/javascript">                         
            $("#submitbtn").click(function() {

            $('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loader.gif" class="loader" />').fadeIn();
            var input_data = $('#wp_signup_form').serialize();
            $.ajax({
            type: "POST",
            url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
            data: input_data,
            success: function(msg){
            $('.loader').remove();
            $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
            }
            });
            return false;

            });
            </script>

            <?php 
                }

            else echo "Registration is currently disabled. Please try again later.";
            ?>

            </div>
            </div>
            <?php

            echo "";
        } //end of if($_post)

}
else {
    wp_redirect( home_url() ); exit;
}
?>

2 个答案:

答案 0 :(得分:0)

使用自定义方法进行注册和邮寄是不安全的。

因此我建议您查看此http://digwp.com/2010/12/login-register-password-code/

我希望这就是你想要的。 最好的。

答案 1 :(得分:0)

正如webfan建议的那样,在主页上重现注册功能并不是一个好主意。更好的方法是从注册页面简单地剪切和粘贴登录表单,并保持表单action指向注册页面:

<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>">
  <!-- registration form HTML goes here -->
</form>