The application I am working on consists of Laravel and Wordpress. I have all the data required in the wordpress database (as additional plugins if need be). I need to be able to connect to the wordpress' wpdb from the laravel controller in order to return the correct view.
however, when I include the wordpress' load.php
(require_once(<wp root>/wp-load.php)
I get the following error when accessing the laravel page:
Cannot redeclare __() (previously declared in /www/laravel/helpers.php:24)
this is because I am trying to use $wpdb to access the DB to get the cotents.
Any ideas on a workaround?
The problem is that wp-load.php
boots most of the WordPress framework and WordPress has a function called __()
. Apparently, so does Laravel. I tried booting wpdb
by itself like this:
include('/path/to/wordpress/wp-includes/wp-db.php');
$mydb = New wpdb('user', 'pass', 'dummydb', 'localhost');
$test = $mydb->get_results("SELECT * FROM {$mydb->posts} LIMIT 5");
But it will throw undefined function errors because it uses functions in the rest of the WordPress code base, which isn't being loaded. That means you aren't going to be able to use $wpdb
and Laravel without that function name conflict.
You really don't need $wpdb
though, at least I don't know why you would. It isn't really much more than a fairly limited wrapper around PHP's mysql_*
functions. It is a (minor) convenience but that is all. If you have database connection information you can do about the same thing with straight PHP.
If you needed to use WP_Query
I'd understand. Some of what it does would be very painful to write by hand.