致命错误:在非对象上调用成员函数get_error_code()

时间:2013-03-14 20:33:15

标签: php wordpress wordpress-plugin salesforce fatal-error

使用我为插件编写的代码时出现以下错误:

错误:致命错误:在第335行的... / wp-login.php中的非对象上调用成员函数get_error_code()

我试过在wp-login.php中做一个var_dump($ errors),它返回NULL。

我的插件:(将注册用户添加到外部数据库)

 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 );

此外,当我使用此功能测试过程时,一切都顺利进行。

  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 );

2 个答案:

答案 0 :(得分:2)

应用过滤器的代码是:

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

...所以你需要从你的函数中返回$ errors对象。目前在您的代码中,只有在您看到异常时才会发生这种情况。你的第二个函数总是返回$ errors,所以它会起作用。

此外,您不需要将$ errors定义为全局,因为它是作为函数参数传递的。

试试这个:

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 );

答案 1 :(得分:0)

您的函数必须返回变量$errors - 即使您的逻辑中没有新错误。

'registration_errors'是一个过滤器,过滤器总是期望返回值。如果没有这个,你的函数会返回NULL,这就是var_dump()找到的。

所以基本功能体应该是:

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

来源:https://wordpress.stackexchange.com/a/90904/33667