2013-03-14 114 views
1

我收到以下錯誤與我寫了我的插件代碼時上:致命錯誤:調用一個成員函數get_error_code()非對象

錯誤:致命錯誤:調用一個成員函數get_error_code()在一個非對象在.../wp-login.php在線335

我試過在wp-login.php中做一個var_dump($ errors)並且它返回NULL。

我的插件:(添加註冊用戶外部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); 

此外,當我測試過程中使用此功能,一切工作進展順利。

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

其應用過濾器的代碼是:

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

...所以你需要返回$錯誤,從你的函數對象。目前在你的代碼中,這隻會在你看到一個異常時纔會發生。你的第二個函數總是返回$ 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); 
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

相關問題