Search and Replace in Joomla

Fix a typo, update a brand name or swap any text across your whole Joomla site at once, using one PHP snippet.

Search and Replace in Joomla
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Your company changed its name, a product got renamed, or the same typo sits in 40 articles and 3 modules. Fixing each one by hand takes an afternoon, and some text, like the output of other extensions, has no field you can edit. You can swap text across your whole site at once with one PHP snippet in Code Snippets.

The PHP snippet code

Right before Joomla sends a page to the browser, the snippet applies every rule in $replacements to the finished HTML.

use Joomla\CMS\Factory;

// Edit your search-and-replace rules here.
$replacements = [
    [
        'search'  => '#Old Company Name#',
        'replace' => 'New Company Name',
    ],
    [
        'search'  => '#old-domain\.com#',
        'replace' => 'new-domain.com',
    ],
];

// Do not edit below.
$app = Factory::getApplication();

$app->registerEvent('onAfterRender', function () use ($app, $replacements)
{
    $document = $app->getDocument();

    // Skip non-HTML responses.
    if (!$document || $document->getType() !== 'html')
    {
        return;
    }

    $body = $app->getBody();

    if (!is_string($body) || $body === '')
    {
        return;
    }

    $newBody = $body;

    // Apply each replacement rule.
    foreach ($replacements as $rule)
    {
        if (empty($rule['search']))
        {
            continue;
        }

        $newBody = preg_replace($rule['search'], $rule['replace'] ?? '', $newBody);
    }

    // Update output only when something changed.
    if ($newBody !== $body)
    {
        $app->setBody($newBody);
    }
});

To add a rule, copy one of the blocks and change search and replace. Each search is a regular expression between # marks:

  • Plain words work as they are, like #Old Company Name#.
  • Put a backslash before dots and other special characters, like #old-domain\.com#.
  • Add i after the closing mark, like #old company name#i, to ignore upper and lower case.

The rules run on the whole page, including link addresses, image alt text and form fields. Keep each search specific enough to match only the text you mean.

Your saved articles stay as they are, because the snippet only changes the page Joomla sends. RSS feeds keep the original text.

How to add this snippet

  1. Install Code Snippets if it isn't installed already.
  2. Log into your Joomla backend.
  3. Go to Components → Tassos Code Snippets.
  4. Click New.
  5. Enter a title, for example Search and replace.
  6. Set Code Type to PHP.
  7. Set Insertion Method to Auto-Insert Locations.
  8. Set Location to On Page Load.
  9. Paste the code above into the editor.
  10. Replace the sample rules with your own.
  11. Click Save & Close.

Check that it works

  1. Open a page that contains the old text.
  2. Look for the new text in its place.

Every page that had the old text should now show the new one, including modules and other extensions' output.

To update links after a move, see Replace Old URLs in Joomla. To learn more about how PHP snippets work, read the PHP snippet docs.

Last updated on Oct 15th 2026 14:10