1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.00 out of 5)
Loading ... Loading ...

integrating wordpress with cakephp

So how do you combine your favorite blogging app with your favorite framework so that user can signin from your cakephp app and maintain his/her session in wordpress, assuming user can only sign up and log in from your cakephp site.

The idea is to create a wordpress user when a user signs up on your cakephp site.

1. We need wordpress password hash function to do this properly. You need to create a new component that would contain wp password hashing functions. The file that contains everything you need is at wp-include/class-phpass.php. Just turn this class into a cakephp component, so you can include it in your user controller.

2. In your user controller, include your recently created wp hash function component. And after the user is saved into your cakephp user table. You want to add it to your wp user table as well. And it would look something like this.


if($this->User->save($this->data))
{
//create blog user for this user
$wp_hash = new $this->Wphash;
$wp_hash->PasswordHash(8,1);
$blog_user['ID'] = $this->User->id;
$blog_user['user_login'] = $this->sanitize_user($email);
$blog_user['user_pass'] = $wp_hash->HashPassword($this->data['User']['password']);
$blog_user['user_email'] = trim($email);
$blog_user['user_url'] = 'http://';
$blog_user['user_nicename'] = $this->data['User']['firstname'].' '.substr($this->data['User']['lastname'],0,1);
$blog_user['display_name'] = $this->data['User']['firstname'].' '.substr($this->data['User']['lastname'],0,1);
$blog_user['user_registered'] = date(configure::read('dateformat'));
$blogdata['Blog_user'] = $blog_user;
$this->Blog_user->save($blogdata);
//create user meta
$bid = $this->Blog_user->id;
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'first_name';
$user_meta['meta_value'] = $this->data['User']['firstname'];
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'last_name';
$user_meta['meta_value'] = $this->data['User']['lastname'];
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'nickname';
$user_meta['meta_value'] = $email;
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'rich_editing';
$user_meta['meta_value'] = 'true';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'comment_shortcuts';
$user_meta['meta_value'] = 'false';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'admin_color';
$user_meta['meta_value'] = 'fresh';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'blog_capabilities';
$user_meta['meta_value'] = 'a:1:{s:10:"subscriber";b:1;}';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

3. In your wordpress index.php file

//change the session name of your wordpress site to match that of your cakephp site.
//so session data can be shared.
session_name("mysite");

4. in your theme header.php(might not be the best place if you change your theme a lot)

//you read the user id
$user = $_SESSION['Auth']['User'];

if(!empty($user))
{
//you change the current user manually
wp_set_current_user($user['id']);
}

And there you have it.

Share:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MySpace
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Twitter
  • Yahoo! Bookmarks
  • #1
    Posted by suthee on July 4th, 2009 at 12:56 AM

    Thanks guys, good info.

  • #2
    Posted by nunpa on July 4th, 2009 at 8:45 AM

    good info.

  • #3
    Posted by JimmyBean on October 1st, 2009 at 11:34 AM

    I don’t know If I said it already but …Cool site, love the info. I do a lot of research online on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, :)

    A definite great read..Jim Bean

  • #4
    Posted by BloggerDude on October 9th, 2009 at 12:11 AM

    I don’t know If I said it already but …I’m so glad I found this site…Keep up the good work I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog. Thanks, :)

    A definite great read….

  • #5
    Posted by Luca Fregoso on April 2nd, 2010 at 11:48 AM

    Great info!thanx a lot!

Share your opinion! Post your thoughts.