简体   繁体   中英

How do I add and log in CakePHP users from Wordpress?

I'm using CakePHP 2.x and Wordpress 4.0.

I want to make Wordpress users automatically from CakePHP users: If a user signs up in CakePHP, then CakePHP instantly inserts the user data into the Wordpress database. Users who log in to CakePHP should be automatically logged in to the Wordpress blog.

See the below code, which i am using in my UsersController in CakePHP 2.x:

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();

        }
    }
    }
}

if ($this->User->save($this->request->data)) {

if ($this->User->save($this->request->data())) { 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM