Support » Plugins » Hacks » Add user via PHP

  • compdrmt


    I searched around and found this issue addressed in a few places but none exactly answered the questions / issues I have so I created a new post. i hope it is in the right place

    First off I am a fairly adept programmer in a number of languages but consider myself maybe mediocre in php but learning quickly.

    What I want to do is have a single login area on my community site and when a person creates a user on the top level I want to either create a user in the other products or use the same table. So when i create a user in my PHP code I want to call a script to create a user with the same info in WordPress. I have found and tried references to wp_create_user in the codex. i am using wordpress 3.0 and have multisite enbaled, so i also tried wpmu_create_user. These have not worked for me both give errors from other files here is the code


    $ident = $_GET [‘pid’];

    $gsql = “select username,password,email from accounts where ident = ‘$ident'”;

    $result = mysql_query($gsql);
    while($row = mysql_fetch_array($result)){

    $wpnum = wp_create_user( $row[‘username’], $row[‘password’], $row[’email’] );

    Originally this yeilded an error calling esc_sql so I added the followng


    to the top now it gives me the following error

    Fatal error: Call to a member function escape() on a non-object in /home/workforc/public_html/campers/campblogs/wp-includes/formatting.php on line 2200

    So the question(s)

    1) have the wp_create_user and wpmu_create_user functions been depracted in 3.0 if so what is the replacment, i can seem to find any other fuunctions

    2) If not are there other files I need to include to make this work, or what am I missing here.

    Alternately or perhaps preferably I would rather just have WP use the table I have set up for the main accounts setup. I have seen an extrension that allowed this but have also read that it does not work in 3.0.

    If neither is possiblle I might have to simply do an insert into the mysql table that WP uses and might need some guidance but would rather rely on it’s one API if possible as it will be way cleaner and less likely to crash everything.

Viewing 8 replies - 1 through 8 (of 8 total)
  • compdrmt


    okay so time for an update.

    I believe it is time to see if i should move past wordpress and look elswhere because after probably 3 dozen hours I am not much closer than when i first started.

    1) I got this to work – not sure why and then then just as strangly it quit working with NO changes to the code.
    2) the only way to get it to work is to load the header which forwards me to the main page of my site whether the add user function workes or not, I would prefer it did not but had resigned myself to being ok if it did load the site, as long as it added the user. As I said it did not add the user. I tried loading just the include files I need but ended up with an error in cache.php that seems to be something which normally just appears in apache logs but in my case echos to the browser and the script dies.
    3)I understand that when using opensource a person is on their own to some degree but the wordpress codex says simply that I must load registrations.php and all will work. Clearly this does not work.
    4) obviously this forum is not moderated or not moderated by anyone who cares about those trying to develop something new, not trying to be mean here just saying that a once over of the forums here and there by someone who knows the product would be nice.



    So to my understanding, you’re using a php based login/registration that is NOT a part of WP3, but you’re trying to hook to the Create User function of WP that will add the input information to the user tables?

    If that’s the case, did you try adding :


    To the head of the php login/registration page to tie it together with wordpress? Then create a re-direct on success or error.

    That’s about all I could suggest without seeing the whole code you’ve got setup to do your functions….



    Yes your inderstanding is correct. I did require the blog header after the initial code and it worked – kinda – for some time it would insert the user in the table but after the line

    $wpnum = wp_create_user( $row[‘username’], $row[‘password’], $row[’email’] );

    it would redirect and output the wordpress site. I would like $wpnum to be inserted into snother table but I had decided i would have to make this happen elsewhere.

    then I put the wp_create_user in to a seperate class and it quit working so I drug out the original files and reverted and it has not worked since. It redirects and outputs the wordpress site but does not even get to the wp_create_user. i have tested this by placing a dir statement which never gets executed and nothing gets posted to my errors log.

    Here is the code I am sure there are many places to be more efficient but this should be plenty readable

    //include_once ('my_wp_class.php');
    require_once('home/workforc/www/campers/campblogs/wp-blog-header.php') ;
    $key = $_GET['acode'];
    $person =  $_GET['uid'];
    $method = $_GET['m'];
    $ssql = "select id,registrant from activations where token = '$key' and registrant= '$person'";
    $result = mysql_query($ssql) ;
    if ($result !=0){
        While( $row = mysql_fetch_array($result)){
            $stamp = gmdate("Y-m-d H:i:s", time());
            $cid = $row["id"];
            $upsql = "update activations set used=1,valid=1,usedat='$stamp',reason='$method' where id =$cid";
                    $reg = $row["registrant"];
                    $upsql = "update accounts set verified=1,active=1 where ident='$reg'";
                    $res3 = mysql_query($upsql);
                        if ($res3 == 1) {
    $ident =  $person;
    // get the user info from the table
    $gsql = "select username,password,email from accounts where ident = '$ident'";
    //echo $gsql;
    $re = mysql_query($gsql) or die(mysql_error());
    //echo $re;
    while ($row = mysql_fetch_array($re)){
    $user= $row['username'] ;
    $em =$row['email'];
    $fname = $row['fname'];
    $lname = $row['lname'];
    // use wp functions to add a user
    // first check if the user exists
    $user_id = username_exists($user) or die (" a horrible death");
    if ( $user_id ) {  // exists so throw error
        if (!isset($errhandler)){
                $errhandler= new apperrors() or die ("cannot create instance of app errors ") ;
              $errhandler->add('User already exists.  Password inherited.',"my_wp_class->add_user");
               if (!isset($errhandler)){
                $errhandler= new apperrors();
              $errhandler->add('User already exists.  Password inherited.',"my_wp_class->add_user") or die("unable to add record to errors - $user_id");
    else{ // does not exist lets create user
         //$user_id = wp_insert_user( array ('user_pass'=>$pass,'user_login'=>$user,'user_email'=>$em)) ;
    $user_id = wp_create_user($user_name, $password, $email) ;
                    // check if there was an error
     if (is_wp_error($user_id)){
                     if (!isset($errhandler)){
                            $errhandler= new apperrors() or die ("unable to creat instance of app errors") ;
                         $errhandler->add($user_id->get_error_message(),"my_wp_class->add_user") or  die("unable to add record to errors - $user_id");;
                // add was succsesful so update our user table
                $isql = "update accounts set wp-userid=$user_id where ident='".$ident. "'";
                $res = mysql_query($isql);
                //return $user_id;




    OK, I see a couple things that are making an issue, but first I’m going to ask a quick question.

    You say you are using WP3 Multisite. When a user is signing up through your php login and you are trying to add their info to the user tables – will the user be signing up for their own sub-blog/user (multisite)?, or just as a ‘user’ off your main site (standard user/member)?

    Both ways require different operations to be preformed, so if you let me know the final outcome of your registration process I can get you in the right direction to actually process it to create a user, or new multi-site (sub-blog with user).




    Thanks for the reply and by the way ignore my early rant. it was born out of frustration after hours spent debugging something that had seemingly worked. I rarely have found the need to ask for help in matters programming, choosing instead to dig through documentation. The notes in the codex are lacking in several places. Nothing in the wp_create_user for example says anything about loading anything besides registrations.php.

    To answers your questions both actually. Let me explain

    I will be teaching classes on blogging and other things. when a person takes one of my classes they will be required to set themselves up as a user on the community site. Others not taking the classes can sign up here as well. I keep track of accounts in a seperate table mostly because there are other peices that come into play (moodle among them) and I want one centralized account creation process With sign up (actually during the activation of the account) they will be made a user in the WP / Buddy press system. So this code is designed to be fired during the account activation process and add the user to wordpress.

    later if the user also signs up for a blogging class a blog will be created for that user. otherwise they will simply be a user on the main site itself and be allowed to comment on blogs, participate in forums etc..

    Thanks again and any help will be greatly appreciated.



    Ok I got it working once again. still not sure why it worked. Based on what I have found the fact that it worked for a while seems a fluke.

    Here is how I made it work

    1) I found that the php file that wants to create a user MUST be loaded to and executed from the top level of the wordpress install, this seemed strange to me I would think the developers would not want thigs stashed into that directory. it might be possible to put it elswhere by playing with the iniset command but I could not make it work.

    2) The call to require or include the file ‘wp-blog-header.php’ must be proceeded by ./ as require_once(./wp-blog-header.php’) or including this file simply redirects output to the main page of your blog. Do not place the ./ before the include call for registration.php.

    3) The order of the includes seems to matter. I have a call to require connect.php which connects to my database so I can get some info needed by this user insert. If I place the include at the very top before anything else my sql query fails. when I change it to the last of the three include files all is well. Strange

    So what I have done is seperated everything into two files one does the updates to my community database and the one in the blog directory concentrates on the worppress add user only. I will call the second one with the exec command and look at the returned value to see how to proceed in the calling file.

    hello compdrmt
    i am working on doing the same things you have done,can you please give me the code of your registration and login page.

    i am trying to achieve the same..i have written the code with the help of code in this post but i am getting an error..can anyone help me out figure where I am doing wrong

    //the include is working fine as i tried to echo from wp-blog-header.php and its working fine
    require_once('D:\wamp/www/d2mail/wp-blog-header.php') ;
    //taken this variable for testing purpose
    $user_name="admin" ;
    $user_id = wp_create_user($user_name, $password, $email) ;
    // check if there was an error
    if (is_wp_error($user_id)){
         if (!isset($errhandler)){
             $errhandler= new apperrors() or die ("unable to creat instance of app errors") ;
                         }          $errhandler->add($user_id->get_error_message(),"my_wp_class->add_user") or  die("unable to add record to errors - $user_id");;
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Add user via PHP’ is closed to new replies.