javascript - how to use jquery validate .js in wordpress for user name already exist?

98

Hi I am making a registration form in myWordPress site

this is the code:

inregister.php

infunctions.php

function reg_register(){

$user_name=esc_attr(stripslashes($_POST['uname']));
$user_email=esc_attr(stripslashes($_POST['email']));
$pass=$_POST['psd'];
$userdata = array(
     'user_login'    =>  $user_name,
        'user_email'  => $user_email,
        'user_pass' =>  $pass
);

 $user_id = wp_insert_user( $userdata ) ;

}

infooter.php

<script type="text/javascript" language="javascript" src="<?php bloginfo('stylesheet_directory');?>/js/jquery.validate.js"></script> 
  <script type="text/javascript">
  $('#register-form').validate();
   </script>

My registration form is working correctly with validation. (i.e. this field is required, please enter a valid emial etc) Now i need to make a validation that this username is already exists. How to do that. Please help

485

Answer

Solution:

On the server side you can validate wether or not a user exists by using the

username_exists($username);

http://codex.wordpress.org/Function_Reference/username_exists.

In addition, inserting a user manually into the database is not recommended as WordPress provides an interface for this functionality via

wp_create_user( $username, $password, $email ); 

http://codex.wordpress.org/Function_Reference/wp_create_user

To see how to implement it in validate.js (you need output the desired javascript , it via php), see http://jquery.bassistance.de/validate/demo/milk/

People are also looking for solutions to the problem: How to create a highchart using mysql data (PHP)?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.