1
我正在使用CakePHP 2.x和Wordpress 4.0。如何從Wordpress添加和登錄CakePHP用戶?
我想從CakePHP用戶自動創建Wordpress用戶:如果用戶在CakePHP中註冊,那麼CakePHP會立即將用戶數據插入到Wordpress數據庫中。登錄到CakePHP的用戶應該自動登錄到Wordpress博客。
請參見下面的代碼,我在CakePHP的2.x的用我的UsersController:符合 if ($this->User->save($this->request->data)) {
代替
include_once($_SERVER['DOCUMENT_ROOT'].'/mycakephpsite/blog/wp-config.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/mycakephpsite/blog/wp-load.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/mycakephpsite/blog/wp-includes/wp-db.php');
App::uses('AppController', 'Controller');
/**
* Users Controller
*
* @property User $User
*/
class UsersController extends AppController {
public function signup() {
$this->layout = '';
if ($this->request->is('post')) {
$this->User->create();
$this->User->set($this->request->data['User']);
$this->request->data['User']['group_id'] = 4;
$this->request->data['User']['status'] = 1;
$success = array();
if ($this->User->validates()){
if ($this->User->save($this->request->data)) {
//create blog user for this user
$website = "http://";
$userdata = array(
'user_login' => 'login_name',
'user_url' => $website,
'user_pass' => $this->data['User']['password'], // When creating an user, `user_pass` is expected.
'user_email' => $this->data['User']['email'], // When creating an user, `user_pass` is expected.
'user_nicename' => $this->data['User']['name'],
'display_name' => $this->data['User']['name']
);
wp_insert_user($userdata) ;
$success['success'] = 'success';
echo json_encode($success);
exit();
}else{
//$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
$success['failed'] = 'failed';
echo json_encode($success);
exit();
}
}else{
$errors = $this->validateErrors($this->User);
//$success['error'] = "error";
$success = $errors;
echo json_encode($success);
exit();
}
}
}
}