Pollora: Integrate WordPress Within a Laravel App

Try Our Free Tools!
Master the web with Free Tools that work as hard as you do. From Text Analysis to Website Management, we empower your digital journey with expert guidance and free, powerful tools.

Pollora emerges as an innovative framework designed to encapsulate WordPress within a Laravel application.

The Laravel framework initializes first, effectively managing front-end requests through its own router, controllers, and Blade templating engine, while simultaneously allowing the WordPress admin interface, database, and plugins to function seamlessly.

The framework presents an array of features:

  • WordPress-aware Routing: Laravel utilizes conditional tags for routing, falling back on the template hierarchy as necessary.
  • Attribute Registration: Declare post types, taxonomies, hooks, and REST routes as PHP attributes.
  • Blade Themes and Blocks: Render Blade views alongside Vite assets and Gutenberg blocks using Blade syntax.
  • Eloquent Models: Access WordPress data via Colt, a specialized fork of Corcel.
  • Media Storage: Leverage any Laravel disk for media libraries, including S3 and R2 integrations.
  • Agent Tooling: Access Pollora guidelines and MCP tools through the Laravel Boost framework.

#Operational Mechanics of Pollora Running WordPress

The framework employs Composer to install WordPress core intopublic/cms, loading it as Laravel initializes, thus utilizing the pre-configured Laravel settings and database connection.

Notably, WordPress retains its request parsing capabilities, ensuring that conditional tags like is_single() continue to function as expected, albeit with Laravel handling the rendering of the responses.

The admin dashboard remains accessible at/cms/wp-admin/, mirroring the standard functionality found on any traditional WordPress site.

With the tandem operation of both frameworks, each web request instantiates WordPress and its associated plugins. Updates to the WordPress core are managed via Composer, diverging from the conventional dashboard update method.

#Routing Leveraging WordPress Conditional Tags

Pollora introduces the methods Route::wp() and Route::wpMatch() into the routing ecosystem.

These methods correspond requests with WordPress conditional tags such as is_page() or is_single(), enabling usage by either the full function name or a brief alias such as page:

use App\Http\Controllers\FormController;use App\Models\Feature;use Illuminate\Support\Facades\Route; Route::wp('single', function () {    return view('post');}); Route::wp('template', 'landing', function () {    return view('page.landing', [        'features' => Feature::all(),    ]);}); Route::wpMatch(['GET', 'POST'], 'page', ['contact', ], [FormController::class, 'index']);

A route is not mandated for every single page. In instances where requests do not align with any defined routes, Pollora resorts to the WordPress template hierarchy to render the corresponding Blade view, meaning that an “About” page would utilize page-about.blade.php if available, subsequently defaulting to page.blade.php.

#Post Types and Hooks as PHP Attributes

Rather than invoking register_post_type() within functions.php, one can declare a post type as a class. Pollora identifies this declaration, auto-generating the slug and labels based on the class nomenclature:

namespace App\Cms\PostTypes; use Pollora\Attributes\PostType;use Pollora\Attributes\PostType\HasArchive;use Pollora\Attributes\PostType\MenuIcon;use Pollora\Attributes\PostType\PubliclyQueryable;use Pollora\Attributes\PostType\Supports; #[PostType]#[PubliclyQueryable]#[HasArchive]#[Supports(['title', 'editor', 'thumbnail'])]#[MenuIcon()]class Event{}

Actions and filters are similarly streamlined, allowing a method to respond to multiple hooks seamlessly:

namespace App\Cms\Hooks; use Pollora\Attributes\Action;use Pollora\Attributes\Filter; class ContentHooks{    #[Action('init', priority: 10)]    #[Action(, priority: 15)]    public function setup(): void    {        // ...    }     #[Filter('the_content', priority: 10)]    public function filterContent(string $content): string    {        return str_replace('ugly', 'shiny', $content);    }}

Command-line tools such as pollora:make:post-type and pollora:make:action facilitate the generation of these classes automatically.

#Gutenberg Blocks Rendered via Blade

The command pollora:make:block scaffolds a new Gutenberg block, creating a block.json, JSX editor files, and a Blade template in one fell swoop:

php artisan pollora:make:block hero-banner --theme

The Blade template continuously renders the Gutenberg block upon each request, ensuring that modifications to the markup cascade across every page utilizing it.

The editor preview derives from the same template, while Pollora streamlines asset construction for the block utilizing Vite.

#Managing Media on Laravel Storage Disks

With Pollora, the WordPress media library can reside on any disk specified within config/filesystems.php. Simply define the disk in your setup, and direct Pollora to it within the .env configuration:

MEDIA_DISK=mediaMEDIA_URL=https://media.example.com

WordPress continues to utilize its native upload functionalities, thereby ensuring that plugins such as ACF and WooCommerce operate without necessitating modifications.

For those unfamiliar with configuring S3 storage in Laravel, consult the documentation on utilizing AWS S3 for Laravel storage.

#Comparative Analysis

Prior endeavors sought to reconcile Laravel and WordPress using diverse methodologies, exemplified by frameworks such as Themosis and Corcel, which permit access to WordPress tables from a Laravel application. Pollora articulates its own comparison against Acorn, Sage, Radicle, and Corcel:

PolloraAcornSageRadicle
DefinitionA Laravel application encapsulating WordPressA module integrating Laravel components within WordPressA foundational WordPress themeA WordPress foundation built on the Roots architecture
Initialization SequenceLaravel precedes, with WordPress nested withinWordPress initializes first, subsequently booting Acorn from a theme or pluginLoaded as a theme within WordPressWordPress initializes first, in conjunction with Acorn
Post Types and HooksEmploys PHP attributesInvolves theme or plugin codeUtilizes theme-based codeConfigured in config/post-types.php
Licensing and PricingMIT, complimentaryMIT, complimentaryMIT, complimentaryOne-time payment

This comparative analysis recommends Sage for developing themes within a conventional WordPress installation, Acorn for integrating Laravel components into existing websites, and Corcel for Laravel applications that merely retrieve WordPress data.

white and blue printer paper

Additionally, it highlights Pollora as an emerging project, contrasting its current usage with the more established status of Acorn and Sage.

#Installation Procedure

To initiate a new project, utilize Composer as follows:

composer create-project pollora/pollora my-project

The installation documentation delineates the prerequisites, setup, Pollora CLI usage, and DDEV configuration details. For developers, the Nectar package adds Pollora guidelines and MCP tools within the Laravel Boost ecosystem.

Pollora falls under an MIT license. Further documentation can be accessed via pollora.dev, and the source code is hosted on the Pollora repository on GitHub.

Source link: Laravel-news.com.

Disclosure: This article is for general information only and is based on publicly available sources. We aim for accuracy but can't guarantee it. The views expressed are the author's and may not reflect those of the publication. Some content was created with help from AI and reviewed by a human for clarity and accuracy. We value transparency and encourage readers to verify important details. This article may include affiliate links. If you buy something through them, we may earn a small commission — at no extra cost to you. All information is carefully selected and reviewed to ensure it's helpful and trustworthy.

Reported By

Souvik Banerjee

I’m Souvik Banerjee from Kolkata, India. As a Marketing Manager at RS Web Solutions (RSWEBSOLS), I specialize in digital marketing, SEO, programming, web development, and eCommerce strategies. I also write tutorials and tech articles that help professionals better understand web technologies.
Share the Love
Related News Worth Reading