如何从用户处获取密码而不是wp-生成-密码

时间:2018-03-24 作者:Mohammad Barghamadi

我很好地使用Wordpress模板和模式登录和注册表,我需要从注册表中的用户那里获取密码,而不是通过wp-generate-password()函数自动生成!

这是我的模式注册表:

<div class="form-wrapper">

<div class="form-heading clearfix">
    <span><?php _e( \'Register\', \'inspiry\' ); ?></span>
    <button type="button" class="close close-modal-dialog" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times fa-lg"></i></button>
</div>

<form id="register-form" action="<?php echo admin_url(\'admin-ajax.php\'); ?>" method="post" enctype="multipart/form-data">

    <div class="form-element">
        <label class="login-form-label" for="register_username"><?php _e( \'Username\', \'inspiry\' ); ?><span>*</span></label>
        <input id="register_username" name="register_username" type="text" class="login-form-input login-form-input-common"
               title="<?php _e( \'* Please enter a valid username.\', \'inspiry\' ); ?>"
               placeholder="<?php _e( \'Username\', \'inspiry\' ); ?>" />
    </div>

    <div class="form-element">
        <label class="login-form-label" for="register_password"><?php _e( \'Password\', \'inspiry\' ); ?><span>*</span></label>
        <input id="register_password" name="register_password" type="password" class="login-form-input login-form-input-common"
               title="<?php _e( \'* Please provide strong password!\', \'inspiry\' ); ?>"
               placeholder="<?php _e( \'Password\', \'inspiry\' ); ?>" />
    </div>

    <div class="form-element">
        <label class="login-form-label" for="register_email"><?php _e( \'Email\', \'inspiry\' ); ?><span>*</span></label>
        <input id="register_email" name="register_email" type="text" class="login-form-input login-form-input-common"
               title="<?php _e( \'* Please provide valid email address!\', \'inspiry\' ); ?>"
               placeholder="<?php _e( \'Email\', \'inspiry\' ); ?>" />
    </div>

    <div class="form-element clearfix">
        <input type="submit" id="register-button" name="user-submit" class="login-form-submit login-form-input-common" value="<?php _e( \'Register\', \'inspiry\' ); ?>" />
        <input type="hidden" name="action" value="inspiry_ajax_register" />
        <?php  // nonce for security
        wp_nonce_field( \'inspiry-ajax-register-nonce\', \'inspiry-secure-register\' );

        if ( is_page() || is_single() ) {
            ?><input type="hidden" name="redirect_to" value="<?php wp_reset_postdata(); global $post; the_permalink( $post->ID ); ?>" /><?php
        } else {
            ?><input type="hidden" name="redirect_to" value="<?php echo esc_url( home_url( \'/\' ) ); ?>" /><?php
        }

        ?>
        <div class="text-center">
            <div id="register-message" class="modal-message"></div>
            <div id="register-error" class="modal-error"></div>
            <img id="register-loader" class="modal-loader" src="<?php echo get_template_directory_uri(); ?>/images/ajax-loader.gif" alt="Working...">
        </div>
    </div>

</form>

<div class="clearfix">
    <span class="login-link pull-left">
        <a href="#" class="activate-section" data-section="login-section"><?php _e( \'Login\', \'inspiry\' ); ?></a>
    </span>
    <span class="forgot-password pull-right">
        <a href="#" class="activate-section" data-section="password-section"><?php _e( \'Forgot Password?\', \'inspiry\' ); ?></a>
    </span>
</div>

这是表单处理程序:

if( !function_exists( \'inspiry_ajax_register\' ) ) :
/**
 * AJAX register request handler
 */
function inspiry_ajax_register() {

    // First check the nonce, if it fails the function will break
    check_ajax_referer( \'inspiry-ajax-register-nonce\', \'inspiry-secure-register\' );

    // Nonce is checked, Get to work
    $info = array();
    $info[\'user_nicename\'] = $info[\'nickname\'] = $info[\'display_name\'] = $info[\'first_name\'] = $info[\'user_login\'] = sanitize_user( $_POST[\'register_username\'] ) ;
    $info[\'user_pass\'] = $_POST[\'register_password\'];
    $info[\'user_email\'] = sanitize_email( $_POST[\'register_email\'] );

    // Register the user
    $user_register = wp_insert_user( $info );

    if ( is_wp_error( $user_register ) ) {

        $error  = $user_register->get_error_codes() ;
        if ( in_array( \'empty_user_login\', $error ) ) {
            echo json_encode( array (
                \'success\' => false,
                \'message\' => __( $user_register->get_error_message( \'empty_user_login\' ) )
            ) );
        } elseif ( in_array ( \'existing_user_login\', $error ) ) {
            echo json_encode ( array (
                \'success\' => false,
                \'message\' => __( \'This username already exists.\', \'inspiry\' )
            ) );

        } elseif ( in_array ( \'existing_user_email\', $error ) ) {
            echo json_encode( array (
                \'success\' => false,
                \'message\' => __( \'This email is already registered.\', \'inspiry\' )
            ) );
        }

    } else {

        //send password as part of email to newly registered user
        inspiry_new_user_notification( $user_register, $info[ \'user_pass\' ]  );

        echo json_encode( array(
            \'success\' => true,
            \'message\' => __( \'\', \'inspiry\' ),
        ) );
    }

    die();

}

// Enable the user with no privileges to request ajax register
add_action( \'wp_ajax_nopriv_inspiry_ajax_register\', \'inspiry_ajax_register\' );
endif;

我在注册表中添加密码输入,并使用$\\u POST[\'register\\u password\']但未工作!是否有更多模板部分我必须更改?你能告诉我怎么了吗?抱歉我的英语不好,提前谢谢!

1 个回复
最合适的回答,由SO网友:Mohammad Barghamadi 整理而成

与从$\\u POST[\'register\\u password\']变量获取密码相关的问题必须在以下括号之间:

// Nonce is checked, Get to work
$info = array();
$info[\'user_nicename\'] = $info[\'nickname\'] = $info[\'display_name\'] = $info[\'first_name\'] = $info[\'user_login\'] = sanitize_user( $_POST[\'register_username\'] ) ;
$info[\'user_pass\'] = ($_POST[\'register_password\']);
$info[\'user_email\'] = sanitize_email( $_POST[\'register_email\'] );

结束

相关推荐

对WordPress的AJAX递归调用返回函数作用域以外的应答

我在使用ajax函数时遇到了一个问题:window.setInterval(liveDrop, 2000); 这会每隔2秒触发我的ajax调用,并检查是否需要更新站点中的某些内容。这个函数运行得很好,但是当我打开类似的东西时functions.php(不仅如此,而且作为示例) echo \'TEST\'; 我的ajax请求已运行window.setInterval 开始打印此“测试”,然后echo teste 实际上超出了ajax请求中“action:my\\u function”的函