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



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

但是,我似乎无法使它起作用。任何帮助将不胜感激。

这是我的代码通过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' );

在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;
    }

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,但是我以这种方式去了。

最新更新