Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Wordpress Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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
    
  2. 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),
        ]);
    }
    
  3. 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,
    ]);
    

Implementation Patterns

1. Integration with Laravel

  • 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
    });
    

2. Common Workflows

  • 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']);
        });
    }
    

3. Database & File Sync

  • 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');
    

4. REST API Integration

  • 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');
    

Gotchas and Tips

Pitfalls

  1. Initialization Timing

    • Issue: WordPress may not load if initialized after routes are cached.
    • Fix: Call WordPress::init() in a service provider’s boot() method before routes are cached.
  2. Database Conflicts

    • Issue: Laravel’s migrations may conflict with WordPress tables.
    • Fix: Use a separate database for WordPress or prefix tables:
      WordPress::init(['table_prefix' => 'wp_']);
      
  3. Plugin/Theme Autoloading

    • Issue: WordPress plugins/themes may not autoload in Laravel.
    • Fix: Manually include them in boot():
      require_once WordPress::plugin_dir() . '/plugin-name/plugin.php';
      
  4. Session Handling

    • Issue: WordPress and Laravel sessions may interfere.
    • Fix: Use separate session drivers or clear WordPress sessions:
      wp_cache_delete('wp_session', 'wp_sessions');
      

Debugging Tips

  • 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');
    

Extension Points

  1. 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'));
            });
        }
    }
    
  2. 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);
    });
    
  3. Multisite Support Configure multisite in WordPress::init():

    WordPress::init([
        'multisite' => true,
        'multisite_config' => [
            'domain_current_site' => 'example.com',
            'path' => '/',
            'subdomains_install' => true,
        ],
    ]);
    
  4. Custom WP_CLI Commands Extend WP_CLI in Laravel:

    if (class_exists('WP_CLI')) {
        WP_CLI::add_command('custom', 'App\Console\Commands\CustomWPCommand');
    }
    

Performance Quirks

  • 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;
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor