I've built a lot of things with Laravel and WordPress. WordPress mainly for blogs, shops and things like that (basically "websites") and Laravel for different applications (mostly applications to organize and manage stuff). Some time ago a client wanted his Laravel application within his WordPress site; so far he just had a link to his application, but no "real" integration - where "real" just means that both components should have the same design. Combining software can sometimes be tricky, but in this case it was surprisingly easy.
Important note!
This does not work with the current versions of Laravel. WordPress and Laravel both use a function named __
(for translation purposes), while WordPress is sadly not wrapping their function within function_exists
. This results in a Cannot redeclare
error and makes it impossible to use this solution.
There's an open ticket in the WordPress issue tracker for this. As long as WordPress is not fixing this, there's sadly no way to get this to work (at least no way I'd be aware of).
The wrong way: use the same design for the Laravel application
An obvious and also pretty easy solution would be to use the same design for Laravel as WordPress - which is bad. Since a lot of the WordPress components like navigation, widgets, etc. aren't static using the same design would cause some serious problems:
- changing your WordPress theme requires you to also change your application design
- every time your WordPress contents change, e.g. a new link in your navigation or changing a widget, you have to adjust your Laravel application
This is quite redundant and therefore bad.
The right way: integrate Laravel into WordPress
A much better (and also pretty easy) solution to this problem is to simply integrate Laravel into WordPress. More specifically: use wordpress functions within your Laravel application.
Assuming you've got a default WordPress installation and a default Laravel installation:
First of all your Laravel application should be within your WordPress directory (or somewhere else, as long as you can easily find it):
wp-admin/
wp-content/
wp-includes/
... other wordpress files ...
app/ <-- laravel application
|-> app/
|-> bootstrap/
|-> config/
|-> public/
|-> ... other laravel files and directories ...
To get WordPress contents within the Laravel application we just need to get access to our WordPress functions (get_header()
, get_footer()
, etc.). For that just open the bootstrap/app.php
within your Laravel installation and add the following line:
require '../../wp-load.php';
That's it. Now we have access to our WordPress functions within Laravel and can alter our (master) view (welcome.blade.php
in this case) to something like:
{!! get_header() !!}
<div class="container">
<div class="content">
<div class="title">Laravel 5</div>
<div class="quote">{{ Inspiring::quote() }}</div>
</div>
</div>
{!! get_footer() !!}
After that our Laravel application should look like this:
One problem that appears now are the missing assets (CSS and JS) of our Laravel application. But we can use the enqueueing functions from WordPress and add the assets we need to our bootstrap/app.php
:
require '../../wp-load.php';
wp_enqueue_style('app', '/app/public/app.css'); // or whatever your css is called
wp_enqueue_script('app', '/app/public/app.js'); // or whatever your js is called
This way assets for the application are just loaded if the application is visited.
As a Service Provider
Following Laravels Service Provider architecture it may be a good idea to write a service provider for including the wp-load.php
and use the boot
function to load assets:
// app/Providers/WordPressServiceProvider.php
class WordPressServiceProvider extends ServiceProvider {
protected $bootstrapFilePath = '../../wp-load.php';
public function boot() {
// Load assets
wp_enqueue_style('app', '/app/public/app.css');
}
public function register() {
// Load wordpress bootstrap file
if(File::exists($this->bootstrapFilePath)) {
require_once $this->bootstrapFilePath;
} else throw new \RuntimeException('WordPress Bootstrap file not found!');
}
}
And add the service provider to the config/app.php
:
/* ... */
'providers' => [
// ...
/*
* Application Service Providers...
*/
// ...
App\Providers\WordPressServiceProvider::class,
Other frameworks
I don't use a lof of other PHP frameworks than Laravel, but including the wp-load.php
to get access to WP functions should work in every framework.