繁体   English   中英

WordPress中的自定义注册表格

[英]Custom registration form in wordpress

我是wordpress新手,我必须在wordpress中创建自定义注册表单。 我听说它可以与PHP和AJAX一起使用。
目前,我的代码如下:(在模板文件中)

jQuery(document).on( "click", '.create-account-btn', function(){

   var inputEmpty = false;
     jQuery(".input__field--yoko").not("[type=submit]").each(function () {
        if (jQuery.trim(jQuery(this).val()).length == 0) inputEmpty = true;
       });
    if (inputEmpty){
        alert('Please fill all fields in the register form');
    } else {
      $.ajax({
        type: "post",
        url: "functions.php",
        dataType: 'json',
        data: {
          action: "registerAccount",
          username: $("#account-username").val(),
          password: $("#account-password").val(),
          email: $("#billing_email").val()
        },
        success:function() {
          alert("I'm created!");
        },
        error: function() {
          alert("Oh no, error! :(");
        }
    });
  }

  return false;
});

(在functions.php文件中)

function registerAccount() {
  if ( isset($_REQUEST) ) {
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password]';
    $email = $_REQUEST['email'];
    $user_id = wp_create_user( $username, $password, $email );

    if (is_int($user_id)) {
      $wp_user_object = new WP_User($user_id);
    }
  }
    die();
}
add_action('wp_ajax_registerAccount', 'registerAccount');

问题是我在AJAX请求中得到错误功能。 这两个文件都在同一个目录中,您有什么线索在这段代码中有什么不好吗?

在Sagar的帮助下,我设法使其正常运行,因此它不再运行错误功能,现在它调用了成功功能,但未创建wordpress帐户,请帮助我。

这是由wp_head灌输的ajaxurl

<?php function frontend_custom_ajaxurl() { ?>
    <script type="text/javascript">
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
    </script>
    <?php
  }
add_action('wp_head','frontend_custom_ajaxurl');

这是您的php函数可以执行的任何操作并将此代码放入functions.php

function your_function() {
    parse_str($_POST['data'], $params);
    print_r($params)
    exit;
}
add_action('wp_ajax_your_function', 'your_function');
add_action('wp_ajax_nopriv_your_function', 'your_function');

这是JQuery。

jQuery(".create-account-btn").submit(function() {
    var data = {
        action: 'your_function', // here php function such as your_function
        data: jQuery(".create-account-btn").serialize(),
    };
    jQuery.post(ajaxurl, data, function(response) {
        ......................
    },'json');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM