Anyone Help With Wordpress (create user)

Discussion in 'PHP' started by lowridertj, Oct 6, 2013.

  1. #1
    I have an ipn script built works great. But i want to send the username, email, pass to my wordpress setup to create a user automatically.

    I have tried many examples online but all seem to fail or missing information on the properly needed

    require/include 's at the top for them to function properly.
     
    lowridertj, Oct 6, 2013 IP
  2. ShlomiKalfa

    ShlomiKalfa Well-Known Member

    Messages:
    418
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #2
    Hey there lowridertj.

    i don't know what IPN script is however if i understand you correctly you are looking for a way to add users to WP automatically through code Right ?
     
    ShlomiKalfa, Oct 7, 2013 IP
  3. ShlomiKalfa

    ShlomiKalfa Well-Known Member

    Messages:
    418
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #3
    If this is what you're looking for here is how the code should look like (php ofc)

    
    // You define an array with the user information you want to enter if i am not mistaken the required fields are Password, email, and user login everything else is pretty much optional
    $users = array(
            "user_pass" => "password",
            "user_login" => "UserLogIn",
            "user_nicename" => "someNickname",
            "user_email" => "userEmail@something.com",
            "user_url" => "something",
            "display_name" => "displayName",
            "nickname" => "nickname",
            "first_name" => "First Name",
            "last_name" => "Last Name",
            "description" => ""
    );
    // Use wp_insert_user with the array you created to create the user. this will either return the id of the newly created user or false(if i am not mistaken)
    $someVariable = wp_insert_user( $user );
    
    PHP:
    Hopes that helps. let me know if you have any more questions.
     
    ShlomiKalfa, Oct 7, 2013 IP
  4. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #4
    before this however, what includes should i reference?
     
    lowridertj, Oct 7, 2013 IP
  5. ShlomiKalfa

    ShlomiKalfa Well-Known Member

    Messages:
    418
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #5
    You should not include anything its a built in WP function. as long as you do the above within WP it will work with no problem

    however if you do this outside of wordpress on a custom script you'll have to do the following:

    // find the real path of your current location
    $fullpath = dirname(__FILE__);
    // if its in another folder you can change the full path to fit by doing something like:
    $fullpath = $fullpath.'/somefolder';
    
    // you check if add_action which is a WP function exists if not then include wp-load.php.
    if (!function_exists('add_action')){
        require_once ($fullpath.'/wp-load.php' );
    }
    PHP:
     
    ShlomiKalfa, Oct 7, 2013 IP
  6. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #6
    thank you ill give this a shot.
     
    lowridertj, Oct 7, 2013 IP
  7. ShlomiKalfa

    ShlomiKalfa Well-Known Member

    Messages:
    418
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #7
    You're welcome. let me know if it worked.
     
    ShlomiKalfa, Oct 7, 2013 IP
  8. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #8
    For some reason the client site i was working on didnt allow me to call the files and execute the functions from another folder (location)
    so ended up using curl, and placed the create account settings in a file in the main root wordpress install.

    thank you for your help. it helped a lot.
     
    lowridertj, Oct 7, 2013 IP
  9. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #9
    > Create account file in root wordpress install (create.php)

    <?php
    global $duser,$dpass;
    require_once("user.php");
    
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $payer_email = $_POST['payer_email'];
    
    
    $someVariable = createUser($first_name.$last_name,$payer_email);
    
    echo $someVariable;
    
    
              $message = "<html>
                                <head><h1>Signup Detail</h1>
                                </head>
                                <body>
                                <p>Signup Detail :</p>
                                <p>Username : ".$duser."</p>
                                <p>Password : ".$dpass."</p>
                                <p>Payer Email : ".$payer_email."</p>
                                <p></p>                          
                                </body>
                              
                            </html>";
            
              //$mail_To = "$payer_email";
              $mail_To = $payer_email;
              $mail_Subject = "WP Signup Detail";
              $mail_Body = $message;
              $headers  = "Content-type: text/html; charset=utf-8 \r\n";
              $headers .= "From: info@yoursite.com\r\n";
              mail($mail_To, $mail_Subject, $mail_Body, $headers);
    
    
    ?>
    PHP:
    > curl script used outside the root wordpress install

    <?php
            /*
          
            CREATE WP USER
          
            */
          
          
    //set POST variables
    $url = 'http://yoursite.com/create.php';
    $fields = array(
            "first_name" => $custname,
            "last_name" => "",
            "payer_email" => $custemail,
    );
    
    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    
    //open connection
    $ch = curl_init();
    
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    
    //execute post
    $result = curl_exec($ch);
    
    //close connection
    curl_close($ch);
    ?>
    PHP:
     
    lowridertj, Oct 7, 2013 IP