无法使用php从前端添加用户

时间:2019-03-08 09:36:42

标签: php jquery ajax wordpress post

我正在仅前端系统上工作,我需要管理员从前端添加用户的方法。

但是我似乎无法使其正常工作。任何帮助将不胜感激。

这是我的代码,我通过AJAX调用它

$( "#createuser" ).click(function() {
  var getusername = $("#usernameset").val();
  var getpassword = $("#userpasswordset").val();
  var getemail = $("#useremailset ").val();
  var getlocation = $(".country_id_set").html();

  adduserfrontend(getusername,getpassword,getemail,getlocation);
});

function adduserfrontend(username,password,email,location) {
  $.ajax ({
      url: "http://localhost/wordpress/wp-content/themes/example/functions.php",
      type: 'POST',
      data: {
          action: 'addnewuser',
          'username': username,
          'password': password,
          'email': email,
          'location': location,
      },
      success: function (results) {
          console.log( 'New user was added' );
      },
      fail: function(data) {
          console.log( data.responseText );
          console.log( 'Request Failed' + data.statusText );
      }
  })
}
<div class="adduser">
<label>Username: </label><input type="text" id="usernameset" name="usernameset" placeholder="New User">
<label>Password: </label><input type="text" id="userpasswordset" name="userpasswordset" placeholder="Enter Password">
<label>Email: </label><input type="text" id="useremailset" name="useremailset" placeholder="example@example.com">
<button id="createuser">Create User</button>
</div>

PHP

function addnewuser() {
  $username = sanitize_text_field( $_POST['username'] );
  $pasword = sanitize_text_field( $_POST['password'] );
  $email = sanitize_text_field( $_POST['email'] );
  $location = sanitize_text_field( $_POST['location'] );
  // add new user
  $user_id = wp_create_user( $username, $pasword, $email );
  $user_id_role = new WP_User($user_id);
  $user_id_role->location($location);

    exit;
}

add_action( 'wp_ajax_addnewuser', 'addnewuser' );

2 个答案:

答案 0 :(得分:0)

在JS代码中使用

url: '<?php echo admin_url( "admin-ajax.php" ); ?>

您还使用了错误的方式在wordpress中调用ajax 使用这种方法

    add_action( 'wp_ajax_addnewuser', 'addnewuser' );
    add_action( 'wp_ajax_nopriv_addnewuser', 'addnewuser' );

    function addnewuser() {
        $username = sanitize_text_field( $_POST['username'] );
  $pasword = sanitize_text_field( $_POST['password'] );
  $email = sanitize_text_field( $_POST['email'] );
  $location = sanitize_text_field( $_POST['location'] );
  // add new user
  $user_id = wp_create_user( $username, $pasword, $email );
  $user_id_role = new WP_User($user_id);
  $user_id_role->location($location);

    exit;
    }

答案 1 :(得分:0)

function user_profile_enqueue() {

    // Register script for localization
    wp_register_script (
        'user-profile-mod',
        get_template_directory_uri() . '/js/example.js',
        array( 'jquery' ),
        '1.0',
        true
    );

    // Localize script so we can use $ajax_url
    wp_localize_script (
        'user-profile-mod',
        'user_meta_ajax',
        array(
            'ajaxurl'   => admin_url( 'admin-ajax.php' )
        )
    );



    // Enqueue script
    wp_enqueue_script( 'user-profile-mod' );

}
add_action( 'wp_enqueue_scripts', 'user_profile_enqueue' );

上面的海报是正确的。我需要包含admin-ajax.php, 但是我是这样处理的。