CakePHP extending WordPress functions

I want to be able to access WordPress’ functions from a CakePHP controller. I tried this from an action:

require_once('path/to/wp-load.php');

It was needed to make a few changes on WP core to avoid re-declaring a couple of functions that WP and CakePHP have in common.

Read More

But I am still getting an error:

Strict (2048): Redefining already defined constructor for class WP_Widget [APPwebrootWPwp-includeswidgets.php, line 93]

Despite the error, if I call The Loop it works. Calling wp_insert_post() also works. Basically I already have access to WP functions. But that redefining error won’t go away.

I tried this:

error_reporting(-1);
Configure::write('debug', 0);

But both didn’t help.

  • Any idea how to get rid of that warning?
  • Is there a correct way to make CakePHP to extend WordPress functions?

I found this component (shama/CakePHP-Wordpress-Component), however the last change was 2 years ago and the author announced 3 months ago that the component is deprecated. Also, it makes use of cURL to interact with WP API. I just want to extend it.

Related posts

Leave a Reply

1 comment

  1. 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.

    1. Install WordPress normally in a sub-folder inside /app/webroot/
    2. In the controller, within the action, you want to use WP functions you call this:

      require_once('full/path/to/wp-load.php');

    3. 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 ... }

    4. 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.