Migrating a WordPress Site with CakePHP
This is an advanced tutorial for WordPress users interested in using WordPress CakePHP. There are several tricks in this tutorial you can apply to your WordPress/CakePHP setup when moving from one server to another. The tricks in this tutorial can be useful whether you are moving to a new server or adding WordPress to your current server with CakePHP on it. Either way, most of the things in this tutorial will need to be done.
Moving a CakePHP/WordPress Site
There are some special precautions to take when moving a WordPress site that has CakePHP installed. First of all is the server configuration. There is often a need to modify the httpd.conf file for your server in order to get the paths to work in your WordPress installation. This is because while a normal server without CakePHP installed has a server root of / while a CakePHP server will have a server root of /app/webroot/. That makes it necessary to alter the line that reads “documentRoot: ….”. You need to change it to have “/app/webroot” after the documentRoot declaration. A simple search in the VI editor at the command prompt for “documentRoot” should do the trick. For those of you not familiar with command line. Here is how to do it on a Linux box or in Putty:
Enter this at command prompt:
sudo vi /etc/httpd/conf/httpd.conf
Be sure to use the correct directory path for your httpd.conf file on your server. That is what is was on mine, but will be different on some servers.
The above command should have opened the VI text editor in Linux with the httpd.conf file opened in it and ready for editing.
Type “I” without quotes to put VI into insert mode.
Use the arrow keys to move down to where the documentRoot declaration is.
Modify the text and hit esc key again to exit insert mode. Then type “:wq” without the quotes to save and exit the file and VI editor.
Restart your server with the following command:
sudo service httpd restart
Moving your WordPress Database
Moving the WordPress database is almost the same as for any WordPress installation. The only small difference is that you may have CakePHP tables to move as well, so look out for those. When you move the database, you have to make some changes to the URLs found in the wp_options table. Simple scroll down the table in browse mode using phpMyAdmin and change any of the site’s old URLs to the new server’s URLs. There is normally at least three that will need changed. Be sure to skim the entire table looking for URLs that are from the old installation and update them in phpMyAdmin. The image below shows one example of a URL that would need changed once the database is on the new server for WordPress to function properly:
Editing CakePHP’s Config Files
In order to make your new WordPress and/or CakePHP database work, go into the app/Config folder on the new server and update the database.php file to reflect your new database connection.
Installing WordPress with CakePHP
Maybe you are not moving a site, but adding WordPress to an existing CakePHP enabled server instead. In that case there is one thing that stops WordPress and CakePHP from working together seamlessly that needs to be addressed in most cases. It is the fact that WordPress uses the double underscore (__) and CakePHP doesn’t allow double underscore conventions. Therefore there is a fix known as the CakePHP WordPress Component that can help. To learn more about it and to see how to install it, go to http://www.kyletyoung.com/code/cakephp_wordpress_component. If you have issues with the double underscore then give this a try.
Where to install WordPress with CakePHP
If you are going to install WordPress with CakePHP, you need to install it in the app/webroot directory for CakePHP to integrate with it. You do not want to put it in the original web root of the server or / because CakePHP rewrites the URLs to all point to app/webroot. Therefore WordPress will not work in the base directory with CakePHP installed on the same server. There are some workarounds if you really want to do this, but it’s not recommended. Besides, what’s the use of having both on the same server if you cannot use CakePHP with WordPress?
Should You Use CakePHP with WordPress
That is a question up for debate. There are some advantages for certain, but there are also disadvantages for some people. It mostly depends on whether you have the patience to learn a framework and whether you know PHP well enough to even bother. CakePHP is nothing more than a PHP framework after all. It was designed to make certain tasks normally done in PHP alone are little easier with helper classes and libraries available through the use of CakePHP. CakePHP is a PHP library, but it is important to distinguish the difference between libraries and frameworks. All PHP frameworks are libraries, but not all libraries are frameworks. A framework implies that it contains an infrastructure to help you do several things in PHP while a library is often a way to accommodate a single need or group of needs in PHP.
If you are good with PHP and looking for a faster way to do tasks such as form validation, database manipulation and enhancing your site’s security, then CakePHP is for you. If you are comfortable with PHP and can do all these things fairly quickly, then you may not want to bother, but if you do these things often, a framework such as CakePHP can certainly speed up your day to day coding. Have a look at the cakePHP manual to see if you think it can help your particular situation or not. I am not a huge fan of frameworks myself, but I try to be fair and give them a chance, so go ahead and give it a try and see if you like it. It might take a day or two to figure it out, but if you end up using it regularly, it will be worth the effort.
For the most part, CakePHP and WordPress do not integrate on many levels to date. However with increasing users, CakePHP is getting more popular and it is likely that WordPress and PHP enthusiasts will find ways to make CakePHP and WordPress work together in more ways in the future. There are several advantages that a good developer could make use of with CakePHP and WordPress combo even though not too many have taken the opportunity to put them together as of the time that this tutorial was written. The reason for this tutorial was not so much to integrate the two infrastructures, so much as to make them work side by side on the same server. It is necessary in cases where you have a CakePHP application on the same server as a WordPress installation where they are both supported by the same root directory. That root directory will need to be that of the CakePHP installation in most cases unless you know some mod-rewrite or .htaccess file tricks. I hope this tutorial helped some of you with your WordPress and CakePHP issues. I know there are a few grey areas. That is why I decided to write on this subject.