It’s safe to say the topic of generative AI doesn’t need an introduction in today’s age. It has been emerging throughout the tech world. However its adoption in the WordPress ecosystem has been slower than in other ecosystems. This is for various, mostly technical reasons that make implementing generative AI features in WordPress harder than elsewhere.
That’s what I’m trying to address with AI Services, a new free open-source plugin that is now available for early access on GitHub, and as an early version 0.1.0 in the WordPress plugin directory.
AI Services is an infrastructure plugin which makes AI centrally available in WordPress, whether via PHP, REST API, JavaScript, or WP-CLI – for any provider. Other plugins can make use of the APIs from the AI Services plugin to easily add AI capabilities to their own plugins, for whichever third party service they prefer: Whether it’s Anthropic, Google, or OpenAI, whether you’d like to use another service, or whether you’d like to leave the choice up to the end users of your plugin – AI Services makes it possible by providing access to the various APIs in a uniform way. It acts as a central hub for integrating AI services, allowing plugin developers to focus on functionality instead of managing individual service integrations. As an end user of WordPress, that means you have more control on how you would like to use the AI capabilities provided by the plugins.
Features
APIs to communicate with any AI service in a uniform way
The AI Services plugin provides an abstraction layer around the various AI services available, and wraps them with an API that other plugins can use in the same way. In other words, if as a plugin you’d like to support any service and leave the choice of which service to use to your plugin’s end users, you still only have to implement the features once. No need to write different code paths conditionally for different third party APIs.
This is a major benefit of relying on an abstraction layer like this. So far, most WordPress plugins with AI capabilities only support a single third party service, most likely the one that the plugin authors are most familiar with. This puts a burden on the end users of WordPress. Imagine you have 10 different plugins with AI features active on your site which use different third party services. You’d be forced to get subscriptions with all of these services just to use the plugins. A much better experience would be that you can choose which service you get a subscription with, and the plugins would simply use whatever you configured. AI Services makes this possible.
The AI Services plugin exposes APIs in both PHP and JavaScript. At the current early stage of the plugin, only text generation is supported, but this will be expanded in the near future to e.g. also support image generation or audio generation. The infrastructure is architected with future support of further capabilities in mind.
For reference, here’s a quick code example for how you could use Google (Gemini) to generate text content in PHP:
if ( ai_services()->is_service_available( 'google' ) ) {
$service = ai_services()->get_available_service( 'google' );
try {
$result = $service
->get_model(
array(
'feature' => 'my-test-feature',
'capabilities' => array( 'text_generation' ),
)
)
->generate_text( 'What can I do with WordPress?' );
} catch ( Exception $e ) {
// Handle the exception.
}
}
Code language: PHP (php)
And the following example shows how you could do the same in JavaScript:
const { hasAvailableServices, getAvailableService } = wp.data.select( 'ai-services/ai' );
if ( hasAvailableServices() ) {
const service = getAvailableService( 'google' );
try {
const result = await service.generateText(
'What can I do with WordPress?',
{ feature: 'my-test-feature' }
);
} catch ( error ) {
// Handle the error.
}
}
Code language: JavaScript (javascript)
If you wanted to accomplish the same task with another AI service like OpenAI, you could do so using the same code, simply replacing google
with openai
.
Or you could omit the AI service identifier entirely, which would mean whichever AI service the end user has configured will be used. You can also pass arguments to filter by certain AI capabilities, such as text generation or image generation, so that only services that satisfy these capabilities would be considered.
Built-in support for popular AI services
The AI Services plugin provides support for the popular services from Anthropic, Google, and OpenAI out of the box. As mentioned before, you’re free to choose whether to use a specific service for your plugin’s AI features or whether to rely on whichever service the end user has configured.
The built-in services are not everything though. The plugin provides a method to register your own AI service implementation, so support for other third party services can be added, e.g. in an extension plugin. If you develop a plugin that complements an AI SaaS product you provide, you could even implement the integration with that SaaS API in that way and register it in the AI Services plugin.
Last but not least, the AI Services plugin provides experimental support for Chrome’s built-in AI, a very promising technology as it makes AI capabilities available for free in the browser, on your own device – no subscriptions required, and no external service involved.
An unopinionated foundation for your AI-powered WordPress plugins
As mentioned, end users of the AI Services plugin have the choice between any of the third party services with built-in support or custom ones. The plugin takes a neutral position and does not favor any particular service. For the built-in services, the rationale to add built-in support for them is their popularity, as well as providing a minimum baseline of AI capabilities out of the box. All of this enhances user choice and user experience: As a user, you paste your API key(s) in a single place, and ideally the AI capabilities on your WordPress site (built on top of the AI Services plugin) reflect that choice and use the configured service going forward.
The AI Services plugin is unopinionated in several other ways: It is free and always will be, you won’t find any ads or upsells in this plugin. It deeply follows the WordPress Core philosophies and reuses WordPress UI components wherever possible for its user interface. It’s not a WordPress Core feature plugin, but it could be one if it was reasonable to add AI capabilities directly to WordPress Core. As of today, we’re not there yet because the lack of standardization and the proprietary and commercial nature of using AI services does not align with criteria that WordPress Core requires. The goal for the AI Services plugin is to fill that gap.
The plugin only comes with a single user-facing AI driven feature, a simple WordPress assistant chatbot, whose purpose is mostly to serve as a proof of concept for using the plugin’s own APIs. You can easily disable that feature if you don’t want to use it. Or as a developer you could write a plugin that replaces it with a more capable assistant. No other user-facing AI features will ever be added to the plugin. That’s because its primary purpose is to provide the infrastructure that enables AI usage in other plugins. While as an end user of WordPress you can install and use the AI Services plugin, it is first and foremost addressed towards other plugin developers.
As a plugin developer, you could choose to build your AI powered plugin on top of the AI Services infrastructure and mark it as a plugin dependency. Or you could add AI capabilities to your plugin conditionally, which would only load if the AI Services plugin is active on the site.
With the APIs the plugin provides, the effort to add AI capabilities to your plugin is reduced from several hours to potentially a few minutes. One of the major drawbacks of implementing your own third party AI API integration in a WordPress plugin is that none of the major services provides a PHP SDK. And while there are JavaScript SDKs for several of them, they are intended for server-side usage (e.g. in a Node.js environment) because using them on the client-side would require the service credentials to be leaked on the client-side – don’t do it. In other words, you would likely need to implement your own PHP API connector. This is part of why most WordPress plugins with AI capabilities today only support a single third party service: No way a plugin developer would want to go through that hassle for multiple different services. With infrastructure like the one provided by the AI Services plugin, you don’t have to worry about this, which notably speeds up and simplifies the process of adding AI features to your plugin.
All of this in the end benefits the end users too. By reducing the friction and barrier of entry for WordPress plugin developers to implement AI capabilities through centralized infrastructure, the enhanced developer experience allows developers to focus on building their AI features in a more user centric way.
Future-proof architecture focused on maintainability
In order to keep the long-term cost of maintenance for this plugin low for myself as well as any potential contributors (pull requests welcome!), I am doing my best to follow various best practices from both the PHP and JavaScript ecosystems.
The AI Services PHP codebase uses an OOP architecture, which helps maintenance and ecosystem interoperability through separation of concerns and clear definition of the public API interface. The latter leads to fewer limitations due to backward compatibility concerns since any code structures not part of the public API can be modified without risk. It’s worth noting that an OOP architecture means onboarding to the project as a contributor can take longer, but in my opinion that’s worth the maintenance win, which at the end comes to the benefits of the developers and users relying on the plugin. For those curious, under the hood it uses a WordPress OOP plugin library that I’ve been working on.
The AI Services JavaScript codebase consists of datastores built on top of @wordpress/data
and React components, all following the latest JavaScript best practices established by WordPress Core and Gutenberg. As explained in the previous section, the plugin uses default components from @wordpress/components
wherever possible, and any custom components still try to mimic the look and feel of WordPress Core’s own UI. I even implemented the plugin’s settings screen using the same foundation that powers the block editor (using @wordpress/interface
), which comes with several accessibility benefits out of the box.
Installation and usage
You can either install the AI Services plugin from the WordPress plugin directory by searching for “AI Services” in your WordPress site, or you can clone the GitHub repository to run it from the source. If you choose to do the latter, you’ll need to run the following commands to build the plugin:
composer install
composer prefix-dependencies
npm install
npm run build
Given the early stage of the plugin and the fact that it is primarily tailored to a plugin developer audience, testing the plugin via GitHub may be the preferred choice at this stage: That way you can easily browse the source code and even decide to contribute back right away if you discover an issue.
In the long term, once the plugin is ready for “prime time”, of course the recommended usage will be via the latest release in the WordPress plugin directory. This also applies if you’re adventurous and want to already run this on a live site today.
Once the plugin is activated, you can find its settings screen under Settings > AI Services, where you can configure the API credentials.
Current development stage and next steps
I want to highlight that the AI Services plugin is still very much a work in progress. It can already be used for certain simple AI tasks, but it is in an early development stage.
I have published an early 0.1.0 release so that the plugin is already available in the WordPress plugin directory, and the idea is to continue publishing new 0.x.y
releases as the AI Services plugin sees further enhancements. Eventually, once a few other of the more crucial features have been added to the plugin and once I have received more feedback on the APIs through actual usage, it should be ready for a 1.0.0 release. It is worth noting that, once the plugin reaches 1.0.0, it will be fully backward-compatible, in line with WordPress Core’s backward compatibility policy. Until then, please use caution when updating to a new 0.x.y
version of the plugin, as sometimes backward compatibility breakage may be unavoidable at this early stage where the public APIs are still being shaped.
My initial focus has been to design a future-proof architecture for this and set up the most foundational text generation capabilities for the built-in AI services, so that it can be used as a proof of concept. Several other AI capabilities that these services provide are not yet implemented, but rest assured they will be. For example, popular AI features like streaming, image generation, or even keeping track of token usage are on the horizon. I have already opened GitHub issues to log some of these features for the future, so feel free to take a look at those. Your feedback will be valuable in better defining those features prior to them being implemented.
Looking for testing and feedback
All of this is just the beginning. As mentioned, the AI Services plugin is still in an early development stage, but it already demonstrates its potential. I’m actively working on expanding its functionalities and am looking for feedback from the community. This is particularly crucial at this early stage, where there’s still a lot of freedom to enhance the APIs with zero backward compatibility restrictions.
If you’re a plugin developer interested in using AI in WordPress, I’d love if you gave AI Services a try. I tried my best in providing early documentation on how to use its developer APIs, so I’d recommend you take a look at that to get started.
If you’re not a developer but curious about the plugin, feel free to directly install it on your WordPress site and explore it. As outlined before, the plugin’s primary purpose is to empower other plugin developers and facilitate easier implementation of AI capabilities, but you may still have valuable feedback on what you can explore using the plugin’s UI.
In any case, feedback via GitHub issues is welcome and much appreciated. If you’re already building prototypes on top of the AI Services plugin APIs, I’d love to see those, so please send them my way in this issue.
Code contributions are welcome too, so please don’t hesitate to open a pull request. The more folks contribute to this project, the better.
I am hopeful that AI Services can significantly help move forward AI integration in WordPress. I’d appreciate if you gave it a try.
Please keep in mind the following:
- This plugin is in its early stages and may experience occasional breaking changes.
- The included WordPress Assistant chatbot serves as a simple proof of concept and can be disabled if unwanted.
Leave a Reply