MG1
MG1

Reputation: 1687

Fatal error: Call to a member function get_error_code() on a non-object

I am getting the following error when with the code that I wrote for my plugin:

Error: Fatal error: Call to a member function get_error_code() on a non-object in .../wp-login.php on line 335

I've tried to do a var_dump($errors) inside wp-login.php and it returns NULL.

My Plugin: (Adds registered users to an external db)

 function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {

 global $SF_USERNAME;
 global $SF_PASSWORD;
 global $errors;

 try {
   $mySforceConnection = new SforceEnterpriseClient();
   $mySoapClient = $mySforceConnection->createConnection(CD_PLUGIN_PATH . 'Toolkit/soapclient/enterprise.wsdl.xml');
   $mySFlogin = $mySforceConnection->login($SF_USERNAME, $SF_PASSWORD);

$sObject = new stdclass();
$sObject->FirstName = $_POST['user_login'];
$sObject->LastName = $_POST['user_login'];
$sObject->Email = $_POST['user_email'];

$createResponse = $mySforceConnection->create(array($sObject), 'Contact');

$ids = array();
    foreach ($createResponse as $createResult) {
        array_push($ids, $createResult->id);
    }

    } catch (Exception $e) {

      $errors->add( 'demo_error', __('<strong>ERROR</strong>: There is a Salesforce problem.','mydomain') );
      return $errors;
     }

 }

 add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );

Additionally, when I test the process with this function, everything works smoothly.

  function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {
    if ( empty( $_POST['first_name'] ) )
        $errors->add( 'first_name_error', __('<strong>ERROR</strong>: You must include a first name.','mydomain') );

    return $errors;
}
 add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );

Upvotes: 2

Views: 4598

Answers (2)

T.Todua
T.Todua

Reputation: 56439

Your function must return the variable $errors – even when there is no new error in your logic.

'registration_errors' is a filter, and filters always expect a return value. Without that your function returns NULL, and that is what var_dump() found.

So the basic function body should be:

function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {
    try {
    } catch (Exception $e) {
        return $errors;
    }
    return $errors; // important!
}

source: https://wordpress.stackexchange.com/a/90904/33667

Upvotes: 0

Simon Hampel
Simon Hampel

Reputation: 2736

The code which applies the filter is:

$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );

... so you need to return the $errors object from your function. Currently in your code, this will only happen if you see an exception. Your second function always returns $errors, so it will work.

Also, you don't need to define $errors as global, since it is being passed in as a function parameter.

Try this:

function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {

    global $SF_USERNAME;
    global $SF_PASSWORD;
    // global $errors; // don't need to do this!

    try {
        $mySforceConnection = new SforceEnterpriseClient();
        $mySoapClient = $mySforceConnection->createConnection(CD_PLUGIN_PATH . 'Toolkit/soapclient/enterprise.wsdl.xml');
        $mySFlogin = $mySforceConnection->login($SF_USERNAME, $SF_PASSWORD);

        $sObject = new stdclass();
        $sObject->FirstName = $_POST['user_login'];
        $sObject->LastName = $_POST['user_login'];
        $sObject->Email = $_POST['user_email'];

        $createResponse = $mySforceConnection->create(array($sObject), 'Contact');

        $ids = array();
        foreach ($createResponse as $createResult) {
            array_push($ids, $createResult->id);
        }

    } catch (Exception $e) {
        $errors->add( 'demo_error', __('<strong>ERROR</strong>: There is a Salesforce problem.','mydomain') );
        // return $errors; // let's move this out of the exception handler block
    }

    return $errors; // always return the $errors object
}

add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );

Upvotes: 2

Related Questions