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.

Read More

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

        }
    }
    }
}

Related posts

Leave a Reply

1 comment