我创建了一个插件,在注册过程中添加了一个函数,用户信息也会添加到外部数据库中。问题是,当函数成功运行时,即用户可以添加到外部db,但wordpress注册过程返回并出错时,用户仍然被添加到外部db。我如何构造它,以便在Wordpress端或外部出现错误时,用户看到并出错,而不会向任何db添加任何内容。
这是我的代码:
function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {
global $SF_USERNAME;
global $SF_PASSWORD;
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) {
//echo $mySforceConnection->getLastRequest();
//echo $e->faultstring;
$errors->add( \'demo_error\', __(\'<strong>ERROR</strong>: There is a Salesforce problem.\',\'mydomain\') );
return $errors;
}
return $errors;
}
add_filter( \'registration_errors\', \'add_user_to_SF\', 10, 3 );