Applies to Joomla 5.x and 6.x · PHP 8.3 · an LLM API (Anthropic, OpenAI or compatible) · developers Updated 16 Oct 2026

Most Joomla sites have dozens of articles with no meta description, or one copied from the global configuration. Search engines then make one up from the page, usually from the wrong paragraph. An LLM writes decent descriptions quickly. The trick is to do it so a person can check them before they go live, and so it doesn't burn tokens on articles that already have a good one.

We do it as a Joomla console command in two steps: suggest writes a CSV, a person reviews and edits it, and apply writes the reviewed lines to the database.

Find the gaps first

# terminal – how big is the job? (adjust the prefix)
mysql -e "SELECT
            SUM(metadesc = '')                    AS empty,
            SUM(CHAR_LENGTH(metadesc) > 160)      AS too_long,
            SUM(CHAR_LENGTH(metadesc) BETWEEN 1 AND 69) AS too_short
          FROM jos_content WHERE state = 1;" mydb

The command

It's registered by a small console plugin, the same pattern as any Joomla console command (see Joomla 6 for extension developers, section 5). The command itself:

// plugins/console/metadesc/src/Command/MetadescCommand.php
namespace Example\Plugin\Console\Metadesc\Command;

use Joomla\CMS\Http\HttpFactory;
use Joomla\Console\Command\AbstractCommand;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\ParameterType;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class MetadescCommand extends AbstractCommand implements DatabaseAwareInterface
{
    use DatabaseAwareTrait;

    protected static $defaultName = 'content:metadesc';

    protected function configure(): void
    {
        $this->setDescription('Suggest meta descriptions with an LLM (suggest), or write reviewed ones (apply)');
        $this->addArgument('mode', InputArgument::REQUIRED, 'suggest | apply');
        $this->addOption('file', null, InputOption::VALUE_REQUIRED, 'CSV file', 'metadesc.csv');
        $this->addOption('limit', null, InputOption::VALUE_REQUIRED, 'Max articles per run', 25);
    }

    protected function doExecute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        return $input->getArgument('mode') === 'apply'
            ? $this->apply($io, $input->getOption('file'))
            : $this->suggest($io, $input->getOption('file'), (int) $input->getOption('limit'));
    }

    private function suggest(SymfonyStyle $io, string $file, int $limit): int
    {
        $db    = $this->getDatabase();
        $query = $db->createQuery()
            ->select($db->quoteName(['id', 'title', 'introtext', 'fulltext']))
            ->from($db->quoteName('#__content'))
            ->where($db->quoteName('state') . ' = 1')
            ->where('(' . $db->quoteName('metadesc') . " = '' OR CHAR_LENGTH(" . $db->quoteName('metadesc') . ') > 160)')
            ->order($db->quoteName('hits') . ' DESC')          // most-read pages first
            ->setLimit($limit);

        $out = fopen($file, 'w');
        fputcsv($out, ['id', 'title', 'metadesc', 'length']);

        foreach ($db->setQuery($query)->loadObjectList() as $article) {
            $text = trim(preg_replace('/\s+/', ' ', strip_tags($article->introtext . ' ' . $article->fulltext)));
            $desc = $this->ask($article->title, mb_substr($text, 0, 6000));
            fputcsv($out, [$article->id, $article->title, $desc, mb_strlen($desc)]);
            $io->writeln(sprintf('#%d (%d) %s', $article->id, mb_strlen($desc), $desc));
        }
        fclose($out);
        $io->success("Review and edit $file, then run: content:metadesc apply --file=$file");

        return Command::SUCCESS;
    }

    private function ask(string $title, string $text): string
    {
        $prompt = "Write a meta description for this web page.\n"
            . "Rules: 140–158 characters. Plain text, no quotes, no emoji, no marketing phrases like 'discover' or 'unlock'. "
            . "Say concretely what the reader gets. Use the page's own terms. Answer with the description only.\n\n"
            . "Title: $title\n\nPage text:\n$text";

        $response = HttpFactory::getHttp()->post(
            'https://api.anthropic.com/v1/messages',
            json_encode([
                'model'      => 'claude-haiku-5-5',
                'max_tokens' => 200,
                'messages'   => [['role' => 'user', 'content' => $prompt]],
            ]),
            [
                'x-api-key'         => getenv('ANTHROPIC_API_KEY'),
                'anthropic-version' => '2023-06-01',
                'content-type'      => 'application/json',
            ],
            30
        );

        if ($response->getStatusCode() !== 200) {
            throw new \RuntimeException('LLM API error ' . $response->getStatusCode() . ': ' . substr((string) $response->getBody(), 0, 200));
        }
        $data = json_decode((string) $response->getBody(), true);

        return trim($data['content'][0]['text'] ?? '');
    }

    private function apply(SymfonyStyle $io, string $file): int
    {
        $db   = $this->getDatabase();
        $in   = fopen($file, 'r');
        $head = fgetcsv($in);
        $n    = 0;

        while (($row = fgetcsv($in)) !== false) {
            [$id, , $desc] = $row;
            $id   = (int) $id;
            $desc = trim($desc);

            if ($desc === '' || mb_strlen($desc) > 160) {
                $io->warning("#$id skipped: empty or longer than 160 characters");
                continue;
            }
            $query = $db->createQuery()
                ->update($db->quoteName('#__content'))
                ->set($db->quoteName('metadesc') . ' = :desc')
                ->where($db->quoteName('id') . ' = :id')
                ->bind(':desc', $desc)
                ->bind(':id', $id, ParameterType::INTEGER);
            $db->setQuery($query)->execute();
            $n++;
        }
        fclose($in);
        $io->success("$n descriptions written. Clear the cache: php cli/joomla.php cache:clean");

        return Command::SUCCESS;
    }
}

The plugin injects the database when it registers the command ($command->setDatabase(...)), and the API key comes from the environment of the shell that runs the command. It never goes into Joomla's configuration or a plugin's settings, where it would sit in the database in plain text.

Running it

# terminal
export ANTHROPIC_API_KEY=…
php cli/joomla.php content:metadesc suggest --limit=25
# open metadesc.csv in a spreadsheet, fix what's wrong, delete rows you don't want
php cli/joomla.php content:metadesc apply --file=metadesc.csv
php cli/joomla.php cache:clean

What makes the descriptions good (or not)

  • The prompt bans the model's habits. Without "no marketing phrases", you get "Discover the ultimate guide to…" on every page. Name the words you don't want.
  • Give it the page, not the title. With only the title, every description becomes a rephrased title. The first 6,000 characters of text are plenty and keep the cost low.
  • Check the length yourself. Models count characters badly, which is why apply refuses anything over 160 and the CSV shows the length.
  • Start with the most-read pages (ORDER BY hits). They're where a better snippet changes clicks.
  • A small, fast model is enough. This is a summarising task. Cost per article is a fraction of a cent.

Why not a plugin that does it on save?

We tried onContentBeforeSave: generate a description whenever an article is saved without one. It works, but it adds an external API call, with its latency and its failures, to the editor's save button, and nobody reviews the result. The batch command keeps the AI out of the request cycle and keeps a person in the loop. If you want it automated, run suggest from the Scheduler weekly and mail the CSV to an editor, but keep apply manual.