Joomla's content plugin system is underused. Most developers reach for components when a simple content plugin hooked into onContentBeforeSave would do the job in a fraction of the code.
This tutorial is a good example of that. The idea is simple: every time an article is saved, we send the text to OpenAI and get back one word, positive, negative, or neutral.
That result gets appended to the meta keywords field and flashed as an admin message. Nothing fancy, but on a community site or news portal where editors are processing dozens of submissions a day, having that sentiment label right in the save workflow saves real time.
Two files, no Composer, no service container. Just a manifest XML and a single PHP class extending CMSPlugin.
What You’ll Need
Before we start, make sure you have:
- Joomla 5.x installed
- PHP 8.1 or newer
- cURL enabled on your server
- An OpenAI API key
Once that’s ready, let’s code.
Step 1: Creation of the Plugin
In your Joomala system, make a new folder within the system under the name of the plugin:
/plugins/content/aisentiment/
Thereupon in that folder generate two files:
- aisentiment.php
- aisentiment.xml
aisentiment.xml
This is the manifest file that the Joomla plugin identifies the identity of this particular plugin and the files that should be loaded into it.
<?xml version="1.0" encoding="utf-8"?><extension type="plugin" version="5.0" group="content" method="upgrade">
<name>plg_content_aisentiment</name>
<author>PHP CMS Framework</author>
<version>1.0.0</version>
<description>Analyze sentiment of comments or articles using OpenAI API.</description>
<files>
<filename plugin="aisentiment">aisentiment.php</filename>
</files>
</extension>
Step 2: Add the PHP Logic
Now let’s write the plugin code.
aisentiment.php
Step 3:Install and activate the Plugin.
Step 4: Test It
This product is out of my expectations and it works excellently!
Bonus Tips:
- Store your API key securely in Joomla’s configuration or an environment variable (not hard-coded).
- Add caching if you’re analyzing large volumes of content.
- Trim long text before sending to OpenAI to save API tokens.
- Handle failed API calls gracefully with proper fallbacks.
Real-World Use Cases:
- Highlight positive user reviews automatically.
- Flag negative feedback for moderation.
- Generate sentiment dashboards for community comments.
Comments · 0