第一个函数需要在Wordpress上注册的用户使用'registration_errors'过滤器将他插入外部数据库。我还需要将Salesforce系统返回的ID插入到Wordpress usermeta表中。最初,我尝试使用$ user_id作为主要wp_insert_user函数的全局来执行update_user_meta($ user_id,'SF_User_ID',$ SFUserID);但是,$ user_id变量没有返回任何内容。现在,我正在尝试在单独的函数中执行update_user_meta,但它需要使用从第一个函数传递的值。当前代码返回此错误:当前代码不起作用,因为没有返回$ error。
我无法弄清楚如何获取Wordpress用户ID并使用Salesforce在$ SFUserID变量中返回的ID更新usermeta表。
function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {
if ( $errors->get_error_code() ) return $errors;
try {
$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);
$SFUserID = $createResult->id;
add_action('user_register', 'addSFUserID', 10, 1);
}
} catch (Exception $e) {
$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 );
function addSFUserID($user_id) {
update_user_meta( $user_id, 'SF_User_ID', $SFUserID );
}
答案 0 :(得分:2)
解决此错误:在非对象上调用成员函数get_error_code()
目前您正在检查$ errors-&gt; get_error_code(),如果$ errors不是wp_error,则get_error_code()方法不存在且会失败。
检查是否有错误,请尝试使用:
is_wp_error( $errors );
http://codex.wordpress.org/Function_Reference/is_wp_error
所以你的if语句看起来像这样:
if ( is_wp_error( $errors ) ) return $errors;
这样一来,如果注册时没有错误,它将继续下面的逻辑。我没有阅读其余的逻辑,但这是我注意到的关于该功能的第一件事。让我知道它是否有效。
问题的第二部分:
这可能会使用很多调整但是为了回答你的问题的第二部分,我认为最好的方法是使用全局变量。我没有测试过这段代码,但看起来应该是这样的:
$SFUserID = '';
function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {
global $SFUserID;
if ( is_wp_error( $errors ) ) return $errors;
try {
$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);
$SFUserID = $createResult->id;
}
} catch (Exception $e) {
$errors->add( 'demo_error', __('<strong>ERROR</strong>: There is a Salesforce problem.','mydomain') );
return $errors;
}
return $errors;
}
function addSFUserID( $user_id ) {
global $SFUserID;
if( !empty( $SFUserID ) )
update_user_meta( $user_id, 'SF_User_ID', $SFUserID );
}
add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );
add_action('user_register', 'addSFUserID', 10, 1);