johnpbloch/wordpress
Composer package for managing WordPress as a dependency. Install specific WordPress core versions via Composer, keep core out of your repo, and integrate WordPress into modern PHP workflows and deployments while retaining the standard WordPress structure.
Installation Add the package to your Laravel project via Composer:
composer require johnpbloch/wordpress
Publish the WordPress core files (if needed):
php artisan vendor:publish --provider="Johnpbloch\WordPress\WordPressServiceProvider" --tag=wordpress
Basic Usage Initialize WordPress in a Laravel service provider:
use Johnpbloch\WordPress\WordPress;
public function boot()
{
WordPress::init([
'root' => storage_path('app/wordpress'),
'debug' => env('WP_DEBUG', false),
]);
}
First Use Case: Querying Posts Use the WordPress API to fetch posts:
$posts = \WP_Query::init()->query_posts([
'post_type' => 'post',
'posts_per_page' => 5,
]);
Service Provider Bootstrapping
Load WordPress early in the Laravel lifecycle (e.g., in AppServiceProvider):
public function boot()
{
WordPress::init([
'root' => storage_path('wordpress'),
'table_prefix' => 'wp_',
'multisite' => false,
]);
}
Middleware for WP Context Restrict WordPress routes to specific middleware:
Route::prefix('wp')->middleware(['web', 'wp_context'])->group(function () {
// WordPress routes or custom endpoints
});
Custom Post Types & Taxonomies
Register them via Laravel’s boot() method:
public function boot()
{
add_action('init', function () {
register_post_type('portfolio', [
'labels' => ['name' => 'Portfolio'],
'public' => true,
]);
});
}
Shortcodes in Blade Use WordPress shortcodes in Laravel Blade templates:
add_shortcode('custom_shortcode', function () {
return '<div>Dynamic content</div>';
});
Widgets & Sidebars Register widgets in a Laravel service provider:
public function boot()
{
add_action('widgets_init', function () {
register_sidebar(['name' => 'Laravel Sidebar']);
});
}
Shared Database
Configure Laravel’s .env to use the same database as WordPress:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=wp_database
DB_USERNAME=wp_user
DB_PASSWORD=wp_password
Uploads Handling Use Laravel’s filesystem to manage WordPress uploads:
$file = $request->file('image');
$path = WordPress::upload_dir()['basedir'] . '/custom-folder/';
$file->storeAs('custom-folder', $file->getClientOriginalName(), 'public');
Extend WordPress REST API Register custom endpoints in Laravel:
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/items', [
'methods' => 'GET',
'callback' => 'get_custom_items',
]);
});
Consume WordPress REST API in Laravel Use Guzzle or Laravel HTTP client:
$response = Http::get('http://your-site/wp-json/wp/v2/posts');
Initialization Timing
WordPress::init() in a service provider’s boot() method before routes are cached.Database Conflicts
WordPress::init(['table_prefix' => 'wp_']);
Plugin/Theme Autoloading
boot():
require_once WordPress::plugin_dir() . '/plugin-name/plugin.php';
Session Handling
wp_cache_delete('wp_session', 'wp_sessions');
Enable WP Debug
WordPress::init(['debug' => true]);
Or via .env:
WP_DEBUG=true
WP_DEBUG_LOG=true
WP_DEBUG_DISPLAY=false
Check WordPress Logs Logs are stored at:
storage_path('logs/wordpress-debug.log')
Disable Caching Temporarily disable object caching:
add_filter('wp_cache_additional_cron', '__return_false');
Custom WordPress Functions
Hook into Laravel’s boot() to add WordPress-specific functions:
public function boot()
{
if (function_exists('add_action')) {
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('custom-style', asset('css/custom.css'));
});
}
}
Laravel Mix + WordPress Assets Compile assets with Laravel Mix and enqueue them in WordPress:
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script('app', mix('js/app.js'), [], null, true);
});
Multisite Support
Configure multisite in WordPress::init():
WordPress::init([
'multisite' => true,
'multisite_config' => [
'domain_current_site' => 'example.com',
'path' => '/',
'subdomains_install' => true,
],
]);
Custom WP_CLI Commands Extend WP_CLI in Laravel:
if (class_exists('WP_CLI')) {
WP_CLI::add_command('custom', 'App\Console\Commands\CustomWPCommand');
}
Avoid init() in Routes
WordPress initialization is heavy; avoid calling it in route closures.
Lazy-Load Heavy Plugins Defer loading resource-intensive plugins:
add_action('wp_loaded', function () {
require_once WordPress::plugin_dir() . '/heavy-plugin/loader.php';
});
Disable Unused WordPress Features Reduce overhead by disabling unused components:
add_filter('option_default_roles', function ($roles) {
$roles['administrator']['show_admin_bar_front'] = false;
return $roles;
});
How can I help you explore Laravel packages today?