Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Wordpress Bridge Bundle Laravel Package

aaronadal/wordpress-bridge-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require aaronadal/wordpress-bridge-bundle
    

    Add the bundle to config/bundles.php:

    return [
        // ...
        Aaronadal\WordpressBridgeBundle\AaronadalWordpressBridgeBundle::class => ['all' => true],
    ];
    
  2. 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.

  3. 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]);
        }
    }
    

Implementation Patterns

Core Workflows

  1. 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']);
    
  2. 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'),
    ]);
    
  3. 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 }
    
  4. Caching Strategies Cache frequent WordPress API calls using Symfony’s cache system:

    $posts = $wordpress->getPosts(['cache' => true, 'cache_ttl' => 3600]);
    

Integration Tips

  • Authentication: Use OAuth2 or JWT for secure API access. Configure in aaronadal_wordpress_bridge.yaml:
    auth:
        method: oauth
        client_id: your_client_id
        client_secret: your_client_secret
        redirect_uri: /wordpress/oauth/callback
    
  • Custom Endpoints: Extend the bundle by creating custom services that wrap WordpressService:
    class CustomWordpressService extends WordpressService
    {
        public function getCustomPosts(array $args = [])
        {
            return $this->getPosts(array_merge($args, ['post_type' => 'custom_post_type']));
        }
    }
    
  • Twig Integration: Pass WordPress data directly to Twig templates:
    {% for post in posts %}
        <h2>{{ post.title.rendered }}</h2>
        <div>{{ post.content.rendered|striptags }}</div>
    {% endfor %}
    

Gotchas and Tips

Pitfalls

  1. 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.

  2. 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'],
        // ...
    ];
    
  3. Deprecated Methods The bundle is based on KayueWordpressBundle, which may have outdated patterns. Prefer the WordpressService methods over legacy services.

  4. 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');
        }
    }
    

Debugging

  • 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());
    }
    

Extension Points

  1. 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]
            );
        }
    }
    
  2. 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);
        }
    };
    
  3. 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'),
    ]);
    
  4. 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 }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views