|
| 1 | +AI generated draft |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# Introducing the PHP AI Client for WordPress |
| 6 | + |
| 7 | +**By [Your Name]** |
| 8 | + |
| 9 | +*Categories: Common APIs, Plugins, Updates* |
| 10 | + |
| 11 | +Since the [WordPress Core AI team](https://wordpress.org/news/2025/05/announcing-the-formation-of-the-wordpress-ai-team/) was formed, one of the most impactful projects they've been developing is the [PHP AI Client](https://github.com/WordPress/php-ai-client). This provider-agnostic SDK is part of the broader [AI Building Blocks for WordPress](https://make.wordpress.org/ai/2025/07/17/ai-building-blocks/) initiative, and it's designed to transform how developers integrate AI capabilities into their plugins and themes. |
| 12 | + |
| 13 | +If you've ever wanted to add AI features to your WordPress plugin but felt overwhelmed by the complexity of managing multiple AI providers, API credentials, and inconsistent interfaces—this post is for you. |
| 14 | + |
| 15 | +## The problem it solves |
| 16 | + |
| 17 | +Today, every WordPress plugin with AI features essentially rebuilds the same infrastructure: provider integrations, API key management, response normalization, and error handling. Users enter the same API credentials multiple times across different plugins and deal with inconsistent experiences. When providers change their APIs, every plugin breaks separately. |
| 18 | + |
| 19 | +The PHP AI Client centralizes this complexity. One integration point handles all providers. One credential system serves all plugins. When providers update or new ones emerge, the changes happen once and benefit everyone. |
| 20 | + |
| 21 | +Think of it like the WordPress HTTP API, but for AI. Instead of every plugin implementing its own HTTP client, WordPress provides a unified interface. The PHP AI Client does the same for AI providers like Anthropic, Google, and OpenAI. |
| 22 | + |
| 23 | +## Two packages, maximum flexibility |
| 24 | + |
| 25 | +The PHP AI Client SDK consists of two Composer packages: |
| 26 | + |
| 27 | +1. **[PHP AI Client](https://github.com/WordPress/php-ai-client)** — A platform-agnostic PHP library that provides the unified AI interface. It's WordPress-agnostic and benefits the broader PHP ecosystem. |
| 28 | + |
| 29 | +2. **[WordPress AI Client](https://github.com/WordPress/wp-ai-client)** — A WordPress-specific wrapper that adds an admin settings screen for API credentials, REST API endpoints, and a JavaScript API for client-side usage. |
| 30 | + |
| 31 | +For most WordPress developers, you'll want to use the WordPress AI Client package, which provides the full experience with admin UI and WordPress-specific integrations. |
| 32 | + |
| 33 | +## Getting started |
| 34 | + |
| 35 | +### Installation |
| 36 | + |
| 37 | +Install the WordPress AI Client via Composer: |
| 38 | + |
| 39 | +```bash |
| 40 | +composer require wordpress/wp-ai-client |
| 41 | +``` |
| 42 | + |
| 43 | +### Configuration |
| 44 | + |
| 45 | +First, initialize the client on the WordPress `init` hook: |
| 46 | + |
| 47 | +```php |
| 48 | +add_action( 'init', function() { |
| 49 | + if ( class_exists( 'WordPress\AI_Client\AI_Client' ) ) { |
| 50 | + WordPress\AI_Client\AI_Client::init(); |
| 51 | + } |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +Then configure your API credentials by navigating to **Settings > AI Credentials** in the WordPress Admin and entering your API keys for the providers you intend to use. |
| 56 | + |
| 57 | +## Building AI-powered features |
| 58 | + |
| 59 | +The SDK provides a fluent `Prompt_Builder` interface that makes constructing AI requests intuitive. Let's look at some practical examples. |
| 60 | + |
| 61 | +### Text generation |
| 62 | + |
| 63 | +Here's a simple example of generating text: |
| 64 | + |
| 65 | +**PHP:** |
| 66 | +```php |
| 67 | +use WordPress\AI_Client\AI_Client; |
| 68 | + |
| 69 | +$response = AI_Client::prompt() |
| 70 | + ->with_user_message( 'Suggest three blog post titles about WordPress development.' ) |
| 71 | + ->for_text_generation() |
| 72 | + ->generate(); |
| 73 | + |
| 74 | +$generated_text = $response->get_text(); |
| 75 | +``` |
| 76 | + |
| 77 | +**JavaScript:** |
| 78 | +```javascript |
| 79 | +const response = await wp.ai.prompt() |
| 80 | + .withUserMessage( 'Suggest three blog post titles about WordPress development.' ) |
| 81 | + .forTextGeneration() |
| 82 | + .generate(); |
| 83 | + |
| 84 | +const generatedText = response.getText(); |
| 85 | +``` |
| 86 | + |
| 87 | +### Image generation |
| 88 | + |
| 89 | +Creating images is just as straightforward: |
| 90 | + |
| 91 | +**PHP:** |
| 92 | +```php |
| 93 | +$response = AI_Client::prompt() |
| 94 | + ->with_user_message( 'A serene mountain landscape at sunset' ) |
| 95 | + ->for_image_generation() |
| 96 | + ->generate(); |
| 97 | + |
| 98 | +$image_url = $response->get_image(); |
| 99 | +``` |
| 100 | + |
| 101 | +### Advanced usage with JSON output |
| 102 | + |
| 103 | +For structured responses, you can request JSON output: |
| 104 | + |
| 105 | +**PHP:** |
| 106 | +```php |
| 107 | +$response = AI_Client::prompt() |
| 108 | + ->with_user_message( 'List 5 SEO improvements for this content...' ) |
| 109 | + ->with_temperature( 0.7 ) |
| 110 | + ->for_text_generation() |
| 111 | + ->with_json_output() |
| 112 | + ->generate(); |
| 113 | +``` |
| 114 | + |
| 115 | +### Multimodal output |
| 116 | + |
| 117 | +The SDK even supports multimodal operations—generating both text and images in a single request: |
| 118 | + |
| 119 | +**PHP:** |
| 120 | +```php |
| 121 | +$response = AI_Client::prompt() |
| 122 | + ->with_user_message( 'Create a product description and promotional image for...' ) |
| 123 | + ->for_text_generation() |
| 124 | + ->for_image_generation() |
| 125 | + ->generate(); |
| 126 | + |
| 127 | +$text = $response->get_text(); |
| 128 | +$image = $response->get_image(); |
| 129 | +``` |
| 130 | + |
| 131 | +## Best practices |
| 132 | + |
| 133 | +### Let the SDK choose the model |
| 134 | + |
| 135 | +By default, the SDK automatically selects a suitable model based on your prompt's requirements and the configured providers on the site. This makes your plugin **provider-agnostic**, allowing it to work on any site regardless of which AI provider the admin has configured: |
| 136 | + |
| 137 | +```php |
| 138 | +// This works regardless of whether the site uses OpenAI, Anthropic, or Google |
| 139 | +$response = AI_Client::prompt() |
| 140 | + ->with_user_message( 'Summarize this article...' ) |
| 141 | + ->for_text_generation() |
| 142 | + ->generate(); |
| 143 | +``` |
| 144 | + |
| 145 | +### Feature detection |
| 146 | + |
| 147 | +Before exposing AI features in your plugin, always check if the required capabilities are available: |
| 148 | + |
| 149 | +**PHP:** |
| 150 | +```php |
| 151 | +$prompt = AI_Client::prompt() |
| 152 | + ->with_user_message( 'Generate an image...' ) |
| 153 | + ->for_image_generation(); |
| 154 | + |
| 155 | +if ( $prompt->is_supported_for_image_generation() ) { |
| 156 | + // Show the AI image generation feature to users |
| 157 | + $response = $prompt->generate(); |
| 158 | +} else { |
| 159 | + // Hide or disable the feature gracefully |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +This ensures your plugin gracefully adapts to environments that may not have AI providers configured. |
| 164 | + |
| 165 | +### Error handling |
| 166 | + |
| 167 | +The SDK offers two approaches for handling errors: |
| 168 | + |
| 169 | +**Exception-based (default):** |
| 170 | +```php |
| 171 | +try { |
| 172 | + $response = AI_Client::prompt() |
| 173 | + ->with_user_message( 'Generate content...' ) |
| 174 | + ->for_text_generation() |
| 175 | + ->generate(); |
| 176 | +} catch ( Exception $e ) { |
| 177 | + // Handle the error |
| 178 | + error_log( 'AI generation failed: ' . $e->getMessage() ); |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +**WP_Error-based:** |
| 183 | +```php |
| 184 | +$response = AI_Client::prompt_with_wp_error() |
| 185 | + ->with_user_message( 'Generate content...' ) |
| 186 | + ->for_text_generation() |
| 187 | + ->generate(); |
| 188 | + |
| 189 | +if ( is_wp_error( $response ) ) { |
| 190 | + // Handle the WP_Error |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## Ideas for AI-powered features |
| 195 | + |
| 196 | +The real magic happens when you think beyond chat interfaces. Here are some features that become surprisingly simple to build with the PHP AI Client: |
| 197 | + |
| 198 | +- **Alternative text suggestions** — Select a paragraph block and generate multiple rephrasing options |
| 199 | +- **Automatic featured images** — Generate images based on post content |
| 200 | +- **SEO optimization** — Analyze content and suggest keyword improvements |
| 201 | +- **Audio versions** — Generate natural-sounding narration of blog posts |
| 202 | +- **Smart media editing** — Add or remove objects from images in the Media Library |
| 203 | +- **Content summarization** — Automatically create excerpts or social media snippets |
| 204 | + |
| 205 | +All of these features would have been complex to build before. With the PHP AI Client, some are nearly trivial to implement. |
| 206 | + |
| 207 | +## Connecting to the bigger picture |
| 208 | + |
| 209 | +The PHP AI Client is designed to work seamlessly with the other AI Building Blocks: |
| 210 | + |
| 211 | +- **[Abilities API](https://developer.wordpress.org/news/2025/11/introducing-the-wordpress-abilities-api/)** — Register your plugin's AI-powered features as discoverable abilities |
| 212 | +- **[MCP Adapter](https://github.com/WordPress/mcp-adapter)** — Allow external AI assistants like Claude and ChatGPT to interact with your WordPress site |
| 213 | +- **[AI Experiments Plugin](https://github.com/WordPress/ai)** — Test and explore all the building blocks together |
| 214 | + |
| 215 | +## Where to learn more and get involved |
| 216 | + |
| 217 | +- Explore the [PHP AI Client GitHub repository](https://github.com/WordPress/php-ai-client) |
| 218 | +- Check out the [WordPress AI Client GitHub repository](https://github.com/WordPress/wp-ai-client) |
| 219 | +- Read the [architecture documentation](https://github.com/WordPress/php-ai-client/blob/trunk/docs/ARCHITECTURE.md) |
| 220 | +- Join discussions in the [#ai-building-blocks](https://wordpress.slack.com/archives/C08TJ8BPULS) channel on WordPress Slack |
| 221 | +- Follow the [Core AI team blog](https://make.wordpress.org/ai/) for updates |
| 222 | + |
| 223 | +## This is just the beginning |
| 224 | + |
| 225 | +The PHP AI Client puts the user first by being provider-agnostic—site administrators choose their preferred AI provider, configure credentials in one place, and all compatible plugins just work. For developers, it means you can focus on building amazing AI features instead of wrestling with infrastructure. |
| 226 | + |
| 227 | +As AI becomes increasingly essential to WordPress, this SDK provides a sustainable foundation that's designed to grow with emerging capabilities—new modalities, advanced features, and novel deployment models. |
| 228 | + |
| 229 | +I'm excited to see what you build with it. |
| 230 | + |
| 231 | +*Props to [@juanmaguitar](https://profiles.wordpress.org/juanmaguitar/), [@bph](https://profiles.wordpress.org/bph/), and [@felixarntz](https://profiles.wordpress.org/flavor/) for feedback and review on this article.* |
| 232 | + |
| 233 | +--- |
| 234 | + |
| 235 | +**Categories:** Common APIs, Plugins, Updates |
| 236 | + |
| 237 | +**Tags:** PHP AI Client, AI, Block development, Extenders |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +This blog post follows the same engaging style as the Abilities API introduction, with: |
| 242 | +- A clear explanation of the problem being solved |
| 243 | +- Practical code examples in both PHP and JavaScript |
| 244 | +- Best practices guidance |
| 245 | +- Creative ideas for implementation |
| 246 | +- Links to resources and community involvement opportunities |
0 commit comments