I found a solution. Not sure if it is the right way to do it. I'm gonna post the whole thing in case someone wants to do the same.
Keep in mind that this was the solution I came up with. I think this is breaking the MVC workflow in so many levels that you should use this workaround at your own risk.
- Install WordPress normally in a sub-folder inside
/app/webroot/
In the controller, within the action, you want to use WP functions you call this:
require_once('full/path/to/wp-load.php');
Call the action to see if there is any conflict. In my case the functions __()
and stripslashes_deep()
existed in both. So, I had to avoid re-declaring them.
if(!function_exists('FUNCTION_NAME_HERE')) { function ... }
At this point, you can already access WP functions inside your controller. But that problem with WP_Widget class will probably fire a warning. So, here is how you fix it:
4.1 Open wp-settings.php
(it is in WP's root) and comment these lines:
require( ABSPATH . WPINC . '/widgets.php' );
$GLOBALS['wp_widget_factory'] = new WP_Widget_Factory();
4.2 Open wp-includes/functions.php
and comment this line:
require_once( ABSPATH . WPINC . '/default-widgets.php' );
4.3 Open wp-content/themes/twentyeleven/inc/functions.php
and comment:
require( get_template_directory() . '/inc/widgets.php' );
You can now call WP normally from inside the action like you would do in a WP theme:
query_posts('showposts=10');
while (have_posts()):
the_title();
endwhile;
Requiring wp-load.php
on beforeFilter()
does not seem to work, unless you do all the WP work inside beforeFilter()
and save the results in a variable. Otherwise CakePHP seems to unload wp-load.php
when moving to the action and you can no longer access WP functions.
It would probably work too if converting this hack into a component and then you could access WP with something like $this->Wordpress->query_posts('showposts=10');
Admin is broken
This is due to step 4, when we commented out all the widgets functions. In order to fix it, you will have to include a condition like this:
if (is_admin()) {
require( ABSPATH . WPINC . '/widgets.php' );
}
This condition, of course, must be applied on all the lines commented in step 4. If you won't use WP admin area, you don't need to use this condition.