Installation Add the bundle to your Laravel/Symfony project via Composer:
composer require blacktrs/symfony-wp-bundle
Register it in config/bundles.php (Symfony) or config/app.php (Laravel) if using a Symfony-based Laravel app:
Blacktrs\SymfonyWpBundle\BlacktrsSymfonyWpBundle::class => ['all' => true],
Publish Config Publish the default configuration:
php artisan vendor:publish --tag=symfony-wp-config
This generates config/symfony_wp.php with default settings for WordPress integration.
First Use Case: Basic WordPress API Access Use the bundle’s service to fetch WordPress data (e.g., posts, users) via Symfony’s HTTP client or Laravel’s HTTP facade:
use Blacktrs\SymfonyWpBundle\Service\WordPressService;
$wpService = app(WordPressService::class);
$posts = $wpService->getPosts(['per_page' => 5]);
Ensure your .env has WordPress REST API credentials:
WP_REST_URL=http://your-wordpress-site/wp-json
WP_REST_USERNAME=admin
WP_REST_PASSWORD=your_password
WordPressService into controllers/services:
public function __construct(private WordPressService $wpService) {}
// app/Facades/WpFacade.php
namespace App\Facades;
use Blacktrs\SymfonyWpBundle\Service\WordPressService;
use Illuminate\Support\Facades\Facade;
class WpFacade extends Facade {
protected static function getFacadeAccessor() { return WordPressService::class; }
}
Usage:
$posts = \App\Facades\WpFacade::getPosts();
$posts = $wpService->getPosts(['per_page' => 5], 300); // Cache for 5 minutes
Configure cache drivers in config/symfony_wp.php:
'cache' => [
'enabled' => true,
'driver' => 'file', // or 'redis', 'database'
'ttl' => 300,
],
// Example: Listen for post updates
$wpService->onPostUpdated(function ($post) {
// Sync with your app's database
});
Configure webhook endpoints in config/symfony_wp.php:
'webhooks' => [
'post_updated' => '/wp-webhook/post-updated',
],
// Example: Convert WP post to a custom resource
$resource = new PostResource($wpService->getPost(123));
return response()->json($resource);
$wpService->setAuth('jwt', [
'token' => 'your_jwt_token',
'expires_at' => now()->addHours(1),
]);
API Rate Limiting
$wpService->setRetryOptions(['max_attempts' => 3, 'delay' => 100]);
CORS Issues
wp-config.php):
define('WP_ALLOW_CORS', true);
Deprecated Endpoints
Symfony vs. Laravel Quirks
HttpClient middleware) may require manual setup. Prefer Laravel’s Http facade for consistency.config/symfony_wp.php:
'debug' => [
'enabled' => true,
'log_requests' => true,
],
storage/logs/laravel.log for failed requests.Custom Endpoints Extend the service to support non-standard WordPress routes:
// app/Services/ExtendedWordPressService.php
class ExtendedWordPressService extends WordPressService {
public function getCustomRoute($endpoint, $params = []) {
return $this->httpClient->get($this->wpRestUrl . $endpoint, $params);
}
}
Middleware Add request/response middleware for logging, auth, or transformation:
// config/symfony_wp.php
'middleware' => [
\App\Http\Middleware\LogWpRequests::class,
],
Testing
Mock the WordPressService in PHPUnit:
$mock = Mockery::mock(WordPressService::class);
$mock->shouldReceive('getPosts')->andReturn([...]);
$this->app->instance(WordPressService::class, $mock);
?per_page=100 for large datasets, then paginate client-side.spatie/image-optimizer to auto-convert WordPress images to WebP.dispatch(new SyncWordPressPosts($wpService))->delay(now()->addMinutes(5));
How can I help you explore Laravel packages today?