Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Building WordPress Plugins with Laravel

WordPress finds and loads every plugin through a single PHP entry file. Laravel 5 gives you reusable components like a service container, which creates and connects your PHP classes. Even when you bring Laravel into a plugin, WordPress still controls when the plugin loads and where your code runs.

The most reliable approach is to keep WordPress in charge and pull in only the Laravel components your plugin uses. The example below uses Laravel 5.5’s container to build a plugin class and attaches that class to a WordPress hook. To make the plugin ZIP installable on any site, you bundle Composer’s runtime dependencies - the packages your code needs to run - directly inside the archive. Because Laravel 5 is a legacy release, verify that the PHP and WordPress versions you plan to support are compatible with it before you start.

Start with WordPress’s plugin entry point

WordPress discovers a plugin by scanning for a PHP file that contains a plugin header, which is a comment block identifying the plugin. That main file loads your dependencies and hooks your code into WordPress, while WordPress continues to handle incoming requests through its own entry point. According to the plugin header requirements, Plugin Name is the only field you must include.

Set up your plugin folder like this:

acme-example/
├── acme-example.php
├── composer.json
├── composer.lock
├── vendor/
├── app/
│   └── AdminNotice.php
└── readme.txt

WordPress expects a plugin directory and a main PHP file, and it ignores Laravel’s usual folder layout. In this structure, app/ holds your plugin’s PHP classes and vendor/ holds the packages Composer installs.

Add a composer.json file that requires the Laravel container and maps your plugin’s namespace to app/:

{
  "name": "acme/example-plugin",
  "type": "project",
  "require": {
    "php": ">=7.0",
    "illuminate/container": "5.5.*"
  },
  "autoload": {
    "psr-4": {
      "Acme\\Example\\": "app/"
    }
  }
}

This configuration pulls in only Laravel 5.5’s container instead of the entire framework. Laravel 5.5 listed PHP 7.0 as its minimum version when it was released, though you should avoid running an obsolete PHP version today. Compare the Laravel 5.5 requirements against the PHP version your target sites actually run.

Load a Laravel component through a WordPress hook

WordPress hooks are named points in the execution flow where your plugin can attach its own code. An action hook runs a callback function when WordPress reaches that point. For instance, admin_notices lets you print a message at the top of the WordPress admin screen. You can read more about how plugins connect to WordPress in the guide to actions and filters.

Place the plugin header and bootstrap code in acme-example.php:

bind(\Acme\Example\AdminNotice::class);

$notice = $container->make(\Acme\Example\AdminNotice::class);

add_action('admin_notices', [$notice, 'render']);

The ABSPATH check stops the file from executing if someone tries to open it directly in a browser instead of letting WordPress load it. Next, Composer’s autoloader makes both your plugin class and Laravel’s container available. The container then instantiates AdminNotice, and add_action() registers its render method with WordPress.

Create the notice class in app/AdminNotice.php:

<?php

namespace Acme\Example;

final class AdminNotice
{
    public function render()
    {
        echo '<div class="notice notice-info"><p>';
        echo esc_html__('Acme Example is active.', 'acme-example');
        echo '</p></div>';
    }
}

In this setup, the container only builds the class, and WordPress decides when to run it. If your plugin needs a settings page, put that logic in separate classes and attach them to WordPress’s settings and admin hooks. Booting a full Laravel application inside a plugin takes much more integration work because Laravel expects to own routing and the request lifecycle. If you actually need a standalone web application, look at the trade-offs in Laravel and WordPress for custom applications.

Package dependencies in the plugin ZIP

Run Composer on your development machine or in your release pipeline. A site owner who uploads your plugin ZIP through the WordPress admin screen should never have to run Composer on their server. Include all runtime dependencies inside the ZIP, including the vendor/ folder and vendor/autoload.php.

Once Composer generates composer.lock, build the production vendor/ directory from those exact locked versions:

composer install --no-dev --prefer-dist --optimize-autoloader

Composer’s basic usage documentation explains how composer install reads composer.lock to install the exact recorded versions, and the command documentation covers flags like --no-dev and --optimize-autoloader.

Bundle composer.json, composer.lock, your plugin files, and the generated vendor/ directory into the ZIP archive. Upload that ZIP to a clean WordPress test site and activate it. If activation throws an error because vendor/autoload.php is missing, your packaging step left out required files.

Keep in mind that other plugins on the same WordPress site also load PHP classes and Composer packages into the same global process. Pick a distinctive namespace, like Acme\Example, and test your plugin alongside the dependencies it expects to use. WordPress’s plugin best practices cover naming and structure, though no single method of isolating dependencies fits every plugin.

Handle activation, deactivation, and removal separately

Register an activation hook when your plugin must run one-time setup tasks, such as creating a custom database table. Use a deactivation hook to clear temporary state when a site owner turns the plugin off. Delete permanent data during uninstall instead of deactivation so that temporarily disabling the plugin does not wipe out the site owner’s saved settings. WordPress details how these three stages differ in its activation and deactivation guidance.

Because the notice example saves no settings or database records, it needs neither an activation routine nor an uninstall script. When you build a plugin that stores data, decide whether uninstalling should remove it and put that cleanup in a dedicated uninstall routine. Run one-time setup tasks inside a lifecycle hook rather than inside a callback that fires on every page load.

Keep WordPress security checks in place

Using Laravel components does not replace WordPress’s security model. Whenever your plugin processes a settings form, check that the current user has permission to change that setting and verify the form’s nonce, which is a token WordPress generates to confirm the request came from an expected screen. After that, sanitize the incoming value before saving it and escape it when you print it back to the page. Remember that a valid nonce does not prove the user is authorized to perform the action. WordPress covers these rules in its security practices and explains the limits of nonces.

In the example above, AdminNotice calls esc_html__() to escape its output. Before you save administrator-level settings from a form, check the user’s capability with a call like current_user_can('manage_options'), validate or sanitize the input for its intended type, and escape the stored value whenever you render it in HTML.

Choose this approach for the right reason

If you are building a small plugin that registers a shortcode or a single admin option, sticking with plain WordPress hooks and APIs keeps the plugin easier to package and maintain. Adding a service container starts to pay off when your plugin grows enough classes and services that wiring them up explicitly keeps the code organized.

Treat Laravel 5 starter repositories as historical references. One WordPress starter project tagged for Laravel 5 used Lumen, Laravel’s micro-framework, and its maintainers note that development has ended, so it is not proof that a full Laravel 5 app works as a drop-in plugin. If you want a maintained way to load Laravel components inside WordPress today, the Acorn installation documentation shows a modern approach, though Acorn targets current Laravel versions rather than Laravel 5.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *