aaronadal/wordpress-bridge-bundle
Installation
composer require aaronadal/wordpress-bridge-bundle
Add the bundle to config/bundles.php:
return [
// ...
Aaronadal\WordpressBridgeBundle\AaronadalWordpressBridgeBundle::class => ['all' => true],
];
Configuration Publish the default config:
php artisan config:publish aaronadal/wordpress-bridge-bundle
Update config/packages/aaronadal_wordpress_bridge.yaml with your WordPress site URL and API credentials.
First Use Case Fetch a WordPress post by ID:
use Aaronadal\WordpressBridgeBundle\Service\WordpressService;
class MyController extends AbstractController
{
public function __construct(private WordpressService $wordpress)
{
}
public function showPost(WordpressService $wordpress, int $id)
{
$post = $wordpress->getPost($id);
return $this->render('post/show.html.twig', ['post' => $post]);
}
}
Data Fetching
Use the WordpressService to retrieve posts, pages, or custom post types:
// Get a single post
$post = $wordpress->getPost(123);
// Get posts by query
$posts = $wordpress->getPosts(['posts_per_page' => 5, 'post_type' => 'product']);
Integration with Symfony Forms Validate and map WordPress data to Symfony forms:
use Aaronadal\WordpressBridgeBundle\Form\WordpressPostType;
$builder->add('title', TextType::class, [
'constraints' => new WordpressPostType('post', 'title'),
]);
Event-Driven Updates Listen for WordPress webhook events (e.g., post updates) via Symfony’s event dispatcher:
// config/services.yaml
services:
App\EventListener\WordpressWebhookListener:
tags:
- { name: kernel.event_listener, event: wordpress.webhook, method: onWebhook }
Caching Strategies Cache frequent WordPress API calls using Symfony’s cache system:
$posts = $wordpress->getPosts(['cache' => true, 'cache_ttl' => 3600]);
aaronadal_wordpress_bridge.yaml:
auth:
method: oauth
client_id: your_client_id
client_secret: your_client_secret
redirect_uri: /wordpress/oauth/callback
WordpressService:
class CustomWordpressService extends WordpressService
{
public function getCustomPosts(array $args = [])
{
return $this->getPosts(array_merge($args, ['post_type' => 'custom_post_type']));
}
}
{% for post in posts %}
<h2>{{ post.title.rendered }}</h2>
<div>{{ post.content.rendered|striptags }}</div>
{% endfor %}
API Rate Limits
WordPress REST API has default rate limits (60 calls/hour for unauthenticated requests). Use caching (cache: true) and authenticated requests to avoid hitting limits.
Data Mismatches WordPress and Symfony data structures differ. Normalize responses before use:
$postData = $wordpress->getPost($id)->toArray();
$normalized = [
'title' => $postData['title']['rendered'],
'content' => $postData['content']['rendered'],
// ...
];
Deprecated Methods
The bundle is based on KayueWordpressBundle, which may have outdated patterns. Prefer the WordpressService methods over legacy services.
CSRF on Webhooks WordPress webhooks may trigger CSRF issues in Symfony. Validate requests manually:
public function onWebhook(Request $request)
{
if (!$request->headers->get('X-Wordpress-Webhook-Signature')) {
throw new \RuntimeException('Invalid webhook signature');
}
}
Enable API Debugging
Add to aaronadal_wordpress_bridge.yaml:
debug: true
Logs will appear in var/log/dev.log.
Check HTTP Status Codes
Use WordpressService::getLastResponse() to inspect raw API responses:
$response = $wordpress->getPost(123);
if (!$response->isSuccess()) {
$this->addFlash('error', 'WordPress API Error: ' . $response->getError());
}
Custom API Endpoints
Extend the WordpressService to handle non-standard endpoints:
class ExtendedWordpressService extends WordpressService
{
public function getCustomEndpoint(string $endpoint, array $data = [])
{
return $this->httpClient->post(
$this->getApiUrl($endpoint),
['json' => $data]
);
}
}
Override Serialization
Modify how WordPress data is serialized/deserialized by overriding the WordpressService constructor or using a decorator:
$service = new class(WordpressService::class) extends WordpressService {
protected function normalizePost(array $post): array
{
// Custom normalization logic
return parent::normalizePost($post);
}
};
Add Custom Fields Register custom WordPress fields in Symfony forms:
use Aaronadal\WordpressBridgeBundle\Form\WordpressCustomField;
$builder->add('custom_field', TextType::class, [
'constraints' => new WordpressCustomField('post', 'custom_field_name'),
]);
Event Listeners
Subscribe to WordPress events (e.g., wordpress.post.created) for real-time updates:
// src/EventListener/WordpressListener.php
class WordpressListener
{
public function onPostCreated(WordpressEvent $event)
{
// Handle new post
}
}
Register in services.yaml:
tags:
- { name: kernel.event_listener, event: wordpress.post.created, method: onPostCreated }
How can I help you explore Laravel packages today?