DomainScoreCursor Rules
GitHub

1/23/2025

A comprehensive guide for developing WordPress plugins using PHP, Guzzle for HTTP requests, and Gutenberg for block editor integration. Follow best practices for coding standards, type hinting, and functional programming paradigms.



# WordPress (PHP, Guzzle, Gutenberg)

# WordPress Plugin Development with PHP, Guzzle, and Gutenberg

## Overview
This guide provides best practices and coding standards for developing WordPress plugins that utilize PHP, Guzzle for HTTP requests, and Gutenberg for block editor integration. The focus is on maintaining readability, consistency, and adherence to WordPress ecosystem best practices.

## Coding Standards
- **PHP**: Always use WordPress coding standards. Type hint all PHP code for better readability and maintainability.
- **JavaScript/TypeScript**: Prefer TypeScript over JavaScript for type safety. Follow WordPress coding standards for both languages.
- **Functional Paradigms**: Favor functional programming over object-oriented programming. Use composition over inheritance where applicable.

## Best Practices
- **Readability**: Optimize code for readability. Use meaningful variable names and consistent formatting.
- **Consistency**: Be consistent with WordPress ecosystem best practices. This includes using WordPress hooks, filters, and REST API endpoints appropriately.
- **Gutenberg Blocks**: When creating new Gutenberg blocks, ensure they are reusable and follow WordPress block development guidelines.

## Tools and Libraries
- **Guzzle**: Use Guzzle for making HTTP requests. Ensure error handling and request optimization are in place.
- **Gutenberg**: Utilize Gutenberg for creating custom blocks. Follow the Gutenberg handbook for best practices.

## Example Code
```php
// Example of a WordPress REST API endpoint using Guzzle
add_action('rest_api_init', function () {
    register_rest_route('myplugin/v1', '/data', array(
        'methods' => 'GET',
        'callback' => 'myplugin_get_data',
    ));
});

function myplugin_get_data() {
    $client = new \GuzzleHttp\Client();
    try {
        $response = $client->request('GET', 'https://api.example.com/data');
        return json_decode($response->getBody(), true);
    } catch (\Exception $e) {
        return new WP_Error('error', $e->getMessage(), array('status' => 500));
    }
}
```

## Conclusion
By following these guidelines, you can develop WordPress plugins that are maintainable, readable, and consistent with the WordPress ecosystem. Always refer to the official WordPress and Gutenberg documentation for the latest best practices and updates.