我必須在註冊的WordPress用戶上寫一個鉤子給Drupal用戶註冊。在提交Wordpress註冊時,它應該將用戶名,密碼等插入到Drupal數據庫中。如何在wordpress中將提交註冊添加到drupal?
drupal版本(7.50)
wordpress版本(4.6.1)。
我必須在註冊的WordPress用戶上寫一個鉤子給Drupal用戶註冊。在提交Wordpress註冊時,它應該將用戶名,密碼等插入到Drupal數據庫中。如何在wordpress中將提交註冊添加到drupal?
drupal版本(7.50)
wordpress版本(4.6.1)。
WordPress的當前活動的主題functions.php文件
add_action('user_register', 'myplugin_registration_save');
function myplugin_registration_save() {
//extract data from the post
//set POST variables
$url = 'http://xxxxxx.com/drupal/drupal_hook_register.php';
$fields = array(
'email' => urlencode($_POST['email']),
'password' => urlencode($_POST['password'])
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
而在Drupal站點根目錄創建一個文件(drupal_hook_register.php )。這個函數將直接將所有的wordpress註冊字段插入到drupal數據庫中。
// define static var
define('DRUPAL_ROOT', getcwd());
// include bootstrap
include_once('./includes/bootstrap.inc');
// initialize stuff
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
//This will generate a random password, you could set your own here
//$password = user_password(8);
//set up the user fields
$fields = array(
'mail' => $email,
'pass' => $password,
'status' => 1,
'init' => 'email address',
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
),
);
//the first parameter is left blank so a new user is created
$account = user_save('', $fields);
//print_r($account);
// If you want to send the welcome email, use the following code
// Manually set the password so it appears in the e-mail.
$account->password = $fields['pass'];
// Send the e-mail through the user module.
//drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', '[email protected]'));
當用戶在wordpress站點註冊時,在wordpress中創建用戶,並使用RESTful web服務將POST值發送到Drupal網站。
REF:https://www.drupal.org/project/restful
用戶登記鉤在WordPress:https://codex.wordpress.org/Plugin_API/Action_Reference/user_register
你能告訴我小例子 – mike
@mike在Drupal實現休息和郵遞員檢查,然後通過CURL發送POST值Drupal的網站。它是一個很大的過程。 https://www.drupal.org/docs/8/api/restful-web-services-api/restful-web-services-api-overview – GNANA
我添加了thr curl函數如何在drupal php頁面中掛鉤用戶插入。我有一個單獨的頁面來做到這一點 – mike