• I’m inserting a user programmatically.

    The problem (from googling) appears to be that the /wp-includes/pluggable isn’t loaded when the form calls the script.

    <?php
      defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );
      $pldir=plugin_dir_path( __FILE__ );
      require $pldir.'regHandler.php';
      if($_POST['button109']){
        // It's a new user registration/
        $newuserdata=regHandler($_POST);
      }

    In the regHandler.php:

    <?php
    function regHandler($formData){
      $email=$formData['email'];
      $returnVals['userid']=0;
      //$pass=wp_generate_password ( 12, false );
      $user_id = wp_insert_user(
        array(
          'user_login'  =>  $email,
          'user_pass' =>  $pass,
          'first_name'  =>  $formData['first_name'],
          'user_nicename'  =>  $formData['first_name'],
          'last_name' =>  $formData['last_name'],
          'user_email'  =>  $email,
          'display_name'  =>  $formData['first_name'] . ' ' . $formData['last_name'],
          'nickname'  =>  $formData['first_name'] . ' ' . $formData['last_name'],
          'role'    =>  'contributor'
        )
      );
    }?>

    The error:
    Fatal error: Call to undefined function wp_hash_password() in /hermes/bosnaweb14a/b1229/ipg/haroldwooduk/wp-includes/user.php on line 1874

Viewing 4 replies - 1 through 4 (of 4 total)
  • The problem (from googling) appears to be that the /wp-includes/pluggable isn’t loaded when the form calls the script.

    You are correct.

    Other files such as: wp-settings.php
    load the file with a line:
    require( ABSPATH . WPINC . '/pluggable.php' );
    Follow up on wp-settings.php to see if it is doing anything else as well as using this line.

    Thread Starter dgcov

    (@dgcov)

    Thanks!

    Where is the best place to put the line?

    I’ve put it in the function.

    function regHandler($formData){
      require_once( ABSPATH . WPINC . '/pluggable.php' );
      $email=$formData['email'];
      $returnVals['userid']=0;
      $user_id = wp_insert_user(
        array(
          'user_login'  =>  $email,
          'user_pass' =>  $pass,
          'first_name'  =>  $formData['first_name'],
          'user_nicename'  =>  $formData['first_name'],
          'last_name' =>  $formData['last_name'],
          'user_email'  =>  $email,
          'display_name'  =>  $formData['first_name'] . ' ' . $formData['last_name'],
          'nickname'  =>  $formData['first_name'] . ' ' . $formData['last_name'],
          'role'    =>  'contributor'
        )
      );

    I would put it just before my function. The file is being “included” into your file, as if the text had been copied in, so it should be at a suitable level, being outside functions or classes etc.

    EDIT: But wait again, we only need it if your function is called, so maybe it should be in your function. Without getting technical, does it work when it is in the function ? If so then leave it there.

    Thread Starter dgcov

    (@dgcov)

    My site has gone down with a white screen.

    Your fix worked but when I went to delete users I had created as part of the testing process, I just got a white screen.

    In the process of fixing that I seem to have borked the site: I’m now getting a Server 500 error.

    I’m trying to fix the site from backups now.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Undefined function wp_hash_password() when instantiating wp_insert_user()’ is closed to new replies.