Accessing WordPress with CakePHP

Bookmark and Share

If you’re like me, you’ve fallen in love with CakePHP. It’s a tight bit of code that’s easy to use and once you have your application using it, easy to maintain and add new features. For my recent redesign of my freelance site www.systemsevendesigns.com I wanted to be able to display my 3 most recent blog posts on the homepage. Both sites run off of different databases, so how do we do it with Cake? (This post assumes you know a little bit about Cake, but is not too terribly complex. Cake newbies welcome!)


First off we will need to figure out a way to connect to our blog database.

In the app\config\database.php add the following bit of code:

 PHP |  copy code |?
01
02
 
03
var $blog = array('driver' => 'mysql',
04
'connect' => 'mysql_connect',
05
'host' => 'mymysqlhost',
06
'login' => 'mymysqlusername',
07
'password' => 'mysqlpassword',
08
'database' => 'wordpress database name',
09
'prefix' => '');
10

After adding the above your app\config\database.php should look similar to this:

 PHP |  copy code |?
01
02
 
03
var $default = array('driver' => 'mysql',
04
'connect' => 'mysql_connect',
05
'host' => 'localhost',
06
'login' => 'thisusername',
07
'password' => 'thispassword',
08
'database' => 'main site db name',
09
'prefix' => '');
10
 
11
var $blog = array('driver' => 'mysql',
12
'connect' => 'mysql_connect',
13
'host' => 'mymysqlhost',
14
'login' => 'mymysqlusername',
15
'password' => 'mysqlpassword',
16
'database' => 'wordpress database name',
17
'prefix' => '');
18
 
19

We are setting up another array containing connection data that can be used by models in our application.



Step two is to create a model that will use this connection to give us data.

Create the file: app\models\blog.php and use the following code:
 PHP |  copy code |?
01
02
class Blog extends AppModel
03
{
04
var $name = 'Blog';
05
var $useDbConfig = 'blog';
06
var $useTable = 'wp_posts';
07
}
08
?>
09
 
10

The two key lines above are
var $useDbConfig = 'blog'; 
which tells us that we want to use the $blog array found in app\config\database.php and
var $useTable = 'wp_posts';
which tells our Blog model to not look for a table called blogs, but one called wp_posts (which is Wordpress’ post table)

Now that we have created a blog model we can query this table just like any other database table. Take this bit of code for example:

 PHP |  copy code |?
01
02
 
03
class SampleController extends AppController{
04
 
05
var $name = 'Sample';
06
var $uses = array('Blog');
07
 
08
function index(){
09
$posts = $this->Blog->findAll(null,null,'post_date DESC',3);
10
$this->set('posts',$posts);
11
}
12
 
13
}
14
?>
15
 
16

The above code will set the $posts variable in my view to be equal to the last 3 posts ordered by post_date descending (or newest to oldest)

Quick Notes: This will not return the categories that the blog is posted in or the number of comments, etc… you would have to create a few other models (similar to like what we have done here) and associate them.

That’s all there is too it. Easy as pie cake!

10 Responses to “Accessing WordPress with CakePHP”

  1. Iain Says:

    Perfect - just what i was looking for - thanks! I’ve got a site currently with wordpress integrated and am now considering migrating the site to Cake.

  2. Carey Baird Says:

    Great tutorial, thanks!

    In order to get only posts, I added a couple of conditions the the findAll call in the controller, as follows:

    $posts = $this->Blog->findAll(array(’post_type’=>’post’, ‘post_status’=>’publish’),null,’post_date DESC’,3);

    Hope it helps someone - I am working on an elegant way to generate a link (using a custom post slug), I will add when I have figured that out.

  3. Iain Mullan Says:

    Thanks! I’d had a go at this already, hadn’t thought of defining a separate DB connection for wordpress - makes sense though.

    One suggestion. If you’re defining a separate connection, then it might be handy to make use of the prefix field, set it to ‘wp_’ , then you can define a model called Post , and there’s no need to redefine var $useTable in the model.

    Would save a bit of time if you want to start using other WP tables as well as wp_posts.

    Cheers!
    Iain

    PS. I just realised comment #1 is from me as well, and ive read this article before :) ….seems like a loooong time ago now. The newly migrated Cake site is http://www.simonsffl.com

  4. Perfect Says:

    Thanks this is just what i needed. I can now start saying goodby to WP
    :)

    thanks
    regards,
    Perfect

  5. Vjhere Says:

    Hi!
    Its really great that i find this wordpress integration with cakephp. but i have stuck at where to place my blog files also how to manage cakephp users table with wp_users table so my application users will also become a blog users.

    Thanks in Advance.

  6. YC Says:

    Is it possible to have WordPress residing in the root, while cakePHP resides in a sub-directory?

    If that’s possible, how would the .htaccess’es looked like for the entire directory tree?

    Thank you for any solution.

  7. WebDevKungfu Blog Archive Accessing WordPress with CakePHP | fix my credit Says:

    [...] WebDevKungfu Blog Archive Accessing WordPress with CakePHP Posted by root 1 hour 2 minutes ago (http://www.webdevkungfu.com) Dec 27 2006 in the app config database php add the following bit of code step two is to create a model that will use this connection to give us data i just realised comment 1 is from me as well and ive read this article before webdevkungfu is proudly powe Discuss  |  Bury |  News | WebDevKungfu Blog Archive Accessing WordPress with CakePHP [...]

  8. Car insurance claims >> http://onlinecarinsuranceclaims.com/ Says:

    [...]www.webdevkungfu.com is another useful authority of information. Online Car insurance claims [...]

  9. Berko Says:

    This is an excellent write up!

    Saved me at least a few hours of figuring it out myself.

    Thanks a lot :)

  10. Jennifer Smith Says:

    I love betting on National Football League. The bad part is that I’ve lost about $ seven this month. I think the most enjoyable part is doing the research and using research to find holes in the odds.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>