---
title: "Automatically Link Twitter (X) @mentions in Joomla Articles - Code Snippets"
description: "Turn every @mention in your Joomla articles into a link to that X profile, so readers can follow the people you write about, using one PHP snippet."
url: "https://www.tassos.gr/docs/code-snippets/howto/content-enhancement/twitter-mention-links"
date: "2026-10-22T23:51:17+00:00"
language: "en-GB"
---

[ Home ](https://www.tassos.gr/docs) / [ Code Snippets ](https://www.tassos.gr/docs/code-snippets) / [ How-to Guides ](https://www.tassos.gr/docs/code-snippets/howto) / [ Content Enhancement ](https://www.tassos.gr/docs/code-snippets/howto/content-enhancement)

#  Automatically Link Twitter (X) @mentions in Joomla Articles

##  Turn every @mention in your Joomla articles into a link to that X profile, so readers can follow the people you write about, using one PHP snippet.

 ![Automatically Link Twitter (X) @mentions in Joomla Articles](https://www.tassos.gr/images/2026/10/code-snippets-twitter-mention-links-card.png) 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.

When you mention someone like @tassosm in an article, readers who want to follow them have to copy the handle and search for it on X. Adding every link by hand is tedious and easy to forget. This PHP snippet turns each @mention in your articles into a link to that X (formerly Twitter) profile, automatically.

## [The PHP snippet code](#the-php-snippet-code)

Set **Code Type** to `PHP`, then use this snippet. It runs while Joomla prepares each article and links every @handle in the visible text to `https://x.com/handle`. The links open in a new tab, and it also works on the article intros on your home page.

```
use Joomla\CMS\Factory;

// Where @mentions link to. Use 'https://twitter.com/' for the old domain.
$profileUrl = 'https://x.com/';

// Do not edit below.

$app = Factory::getApplication();

$linkMentions = function ($event) use ($profileUrl)
{
    // Joomla 5 and later pass the item through getItem(), Joomla 4 as an argument.
    $item = method_exists($event, 'getItem')
        ? $event->getItem()
        : $event->getArgument(1);

    if (!is_object($item)
        || empty($item->text)
        || strpos($item->text, '@') === false)
    {
        return;
    }

    // @handle: up to 15 letters, digits or underscores,
    // and not part of an email address, URL or domain.
    $pattern = '#(?<![A-Za-z0-9_@/.:])@([A-Za-z0-9_]{1,15})'

        . '(?![A-Za-z0-9_@]|\.[A-Za-z0-9_])#';

    $toLink = function ($match) use ($profileUrl)
    {
        $url  = $profileUrl . $match[1];
        $attr = 'target="_blank" rel="noopener"';

        return '<a href="' . $url . '" ' . $attr . '>@' . $match[1] . '</a>';
    };

    // Split the HTML into tags and text, so only visible text gets links.
    $parts = preg_split('#(<[^>]*>)#', $item->text, -1, PREG_SPLIT_DELIM_CAPTURE);

    $skip  = 0;

    foreach ($parts as $i => $part)
    {
        if ($part === '')
        {
            continue;
        }

        // Leave text inside links, code blocks, scripts and form fields alone.
        if ($part[0] === '<')
        {
            $tags = 'a|code|pre|script|style|textarea';

            if (preg_match('#^<(/?)(' . $tags . ')\b#i', $part, $tag))

            {
                $skip = max(0, $skip + ($tag[1] === '/' ? -1 : 1));
            }

            continue;
        }

        if ($skip > 0)
        {
            continue;
        }

        $parts[$i] = preg_replace_callback($pattern, $toLink, $part);
    }

    $item->text = implode('', $parts);
};

$app->registerEvent('onContentPrepare', $linkMentions);
```

The snippet only changes plain text. It leaves these alone:

- Email addresses.
- Existing links, so a mention you already linked by hand stays as it is.
- Code blocks.
- Image alt text and other HTML attributes.
- URLs and domain names, like a Mastodon profile link.
- Handles longer than 15 characters, which X doesn't allow.

The links are added while the page is built, so your saved articles stay exactly as you wrote them. Unpublish the snippet and the mentions go back to plain text.

To link to twitter.com instead, change `$profileUrl` to `'https://twitter.com/'`.

## [How to add this snippet](#how-to-add-this-snippet)

1. Install [Code Snippets](https://www.tassos.gr/docs/code-snippets/getting-started/how-to-install) 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 *Twitter mention links*.
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. Click **Save & Close**.

## [Check that it works](#check-that-it-works)

1. Open an article that mentions someone with an @handle.
2. Hover over the mention.

It should now be a link to that person's X profile.

For other automatic text changes, see [Search and Replace in Joomla](https://www.tassos.gr/docs/code-snippets/howto/content-enhancement/search-and-replace-joomla). To learn more about how PHP snippets work, read the [PHP snippet docs](https://www.tassos.gr/docs/code-snippets/types/php).

 Last updated on Oct 15th 2026 14:10
