wpstarter/framework
WPStarter Framework is a Laravel-inspired PHP framework for building WordPress apps and plugins with modern patterns. It provides familiar helpers, service container features, and a clean structure to speed development while staying compatible with WordPress.
Installation Add the package via Composer:
composer require wpstarter/framework
Publish the configuration (if needed):
php artisan vendor:publish --provider="WpStarter\Framework\ServiceProvider"
Bootstrapping WordPress
Register the WordPress bootstrap in app/Providers/AppServiceProvider.php:
use WpStarter\Framework\Facades\WordPress;
public function boot()
{
WordPress::boot();
}
First Use Case: Accessing WordPress Data Fetch a post by ID in a Laravel controller:
use WpStarter\Framework\Facades\WordPress;
public function showPost($id)
{
$post = WordPress::post()->find($id);
return response()->json($post);
}
Key Facades
WordPress: Core facade for bootstrapping and utilities.WordPress::post(): Interact with posts.WordPress::user(): Manage users.WordPress::option(): Handle options.Extend Laravel controllers to interact with WordPress:
use WpStarter\Framework\Facades\WordPress;
class PostController extends Controller
{
public function index()
{
$posts = WordPress::post()->all(['posts_per_page' => 10]);
return view('posts.index', compact('posts'));
}
}
Create a service to abstract WordPress logic:
class PostService
{
public function __construct(protected $wpPost)
{
$this->wpPost = WordPress::post();
}
public function getPublishedPosts()
{
return $this->wpPost->all(['post_status' => 'publish']);
}
}
Restrict routes to logged-in WordPress users:
use WpStarter\Framework\Facades\WordPress;
class WordPressAuthMiddleware
{
public function handle($request, Closure $next)
{
if (!WordPress::user()->isLoggedIn()) {
abort(403);
}
return $next($request);
}
}
Hook into WordPress events (e.g., wp_loaded):
use WpStarter\Framework\Facades\WordPress;
WordPress::on('wp_loaded', function () {
// Custom logic after WordPress loads
});
Extend Blade to output WordPress data:
Blade::directive('wpPost', function ($expression) {
return "<?php echo WpStarter\Framework\Facades\WordPress::post()->find({{$expression}})->title; ?>";
});
Usage:
<h1>{{ wpPost(1) }}</h1>
Leverage Laravel’s API resources:
Route::get('/posts', function () {
return PostResource::collection(WordPress::post()->all());
});
Bootstrap Timing
WordPress::post() in service providers or before boot() in AppServiceProvider.WordPress::boot() is called in boot() (not register()).Caching Conflicts
php artisan cache:clear
wp cache flush # If using WP-CLI
Query Conflicts
$wpdb) with Eloquent queries in the same request. Use transactions or separate them clearly.Plugin/Theme Overrides
Multisite Limitations
wp-config.php for WP_ALLOW_MULTISITE and test manually.Enable WordPress Debugging
Add to wp-config.php:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true); // Logs to /wp-content/debug.log
Laravel-WordPress Logs Use Laravel’s logging for hybrid issues:
\Log::info('WordPress post ID:', ['id' => $post->ID]);
Check Hooks/Filters
Use WordPress::hasAction('hook_name') to verify if a hook exists before adding listeners.
Custom WordPress Facades Extend the package by creating your own facades:
class CustomWordPress extends \WpStarter\Framework\Facades\WordPress
{
public static function customMethod()
{
return self::post()->find(1)->custom_field;
}
}
Override WordPress Classes
Use Laravel’s binding system to replace WordPress classes (e.g., WP_Query):
$this->app->bind('wp_query', function () {
return new CustomWPQuery();
});
Add Custom WPStarter Services Register additional services in the config:
'services' => [
'custom' => \WpStarter\Framework\Services\CustomService::class,
],
Then access via:
WordPress::service('custom')->doSomething();
Modify Query Parameters Extend the query builder for custom WordPress queries:
WordPress::extend('post', function ($query) {
$query->addParam('custom_param', 'value');
return $query;
});
Lazy-Load WordPress Defer WordPress bootstrapping until needed (e.g., in a middleware or route group):
Route::middleware(['wp.bootstrap'])->group(function () {
// Routes requiring WordPress
});
Batch Queries
Use WordPress::post()->batch() for large datasets to reduce memory usage.
Cache WordPress Data Cache frequent WordPress queries in Laravel’s cache:
$posts = Cache::remember('wp_posts', now()->addHours(1), function () {
return WordPress::post()->all();
});
How can I help you explore Laravel packages today?