-
-
- Submit Updated Articles to Bing and Yandex with IndexNow in Joomla
- Add Published and Modified Date Meta Tags to Joomla
- Add Open Graph and Twitter Card Tags to Joomla
- Auto-Generate Meta Descriptions from Article Text in Joomla
- Add Noindex to Specific Pages in Joomla
- Verify Your Joomla Site with Google Search Console
-
- Add a View as Markdown Link to Joomla Articles
- Display the Last Modified Date in Joomla Articles
- Embed Iframes in Joomla
- Automatically Link Twitter (X) @mentions in Joomla Articles
- Open External Links in a New Tab in Joomla
- Display Estimated Reading Time in Joomla Articles
- Replace Old URLs in Joomla
- Search and Replace in Joomla
-
- Add a Footer Line to the Joomla Administrator
- Show a Staging Banner in the Joomla Administrator
- Remove the "All" Option from Admin List Limits in Joomla
- Add a Cookie Consent Banner to Joomla
- Make the Administrator Navigation Sticky in Joomla
- Add Google Fonts to Joomla
- Add a Facebook Messenger Chat Button to Joomla
- Add a Reading Progress Bar to Joomla
- Add a Custom Scrollbar to Joomla
- Show Confetti on a Thank You Page in Joomla
- Add Smooth Scrolling to Joomla
- Add a Back to Top Button in Joomla
- Add a WhatsApp Chat Button in Joomla
-
- Protect the Administrator with a Secret URL Key in Joomla
- Show a Welcome Message on First Login in Joomla
- Log Out Idle Administrators in Joomla
- Assign New Users to a Different User Group Automatically in Joomla
- Redirect Visitors by Country in Joomla
- Redirect Users After Login by User Group in Joomla
- Block User Registration with Certain Usernames in Joomla
- Block User Registration by Country in Joomla
- Block User Registration by Email Address in Joomla
- Block User Registration by IP Address in Joomla
- Remove the Forgot Password and Username Links in Joomla
- Remove the Generator Meta Tag in Joomla
- Let Users Log In with Their Email Address in Joomla
- Block Traffic by Country in Joomla
- Block Bot Traffic in Joomla
- Block Traffic by IP Address in Joomla
Let Users Log In with Their Email Address in Joomla
Let your users log in with their email address or their username, so nobody gets stuck at your Joomla login form, using one PHP snippet.
Joomla's login form only accepts a username. Most people remember their email address instead, so they type that, get "Username and password do not match", and email you for help. You could install an authentication plugin to fix it, or you can add one PHP snippet with Code Snippets.
The PHP snippet code
Set Code Type to PHP, then use this snippet. When someone submits the login form with an email address, it looks up the account that owns that email and hands its username to Joomla. Joomla's own login then runs as usual, so it still checks the password and every other login rule.
It also covers the last step of a password reset. Joomla emails a verification code, then asks for the username along with that code. With this snippet, users can type their email there too.
use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
$app = Factory::getApplication();
$input = $app->getInput();
if ($input->getMethod() !== 'POST' || $input->getCmd('option') !== 'com_users')
{
return;
}
$task = $input->getCmd('task');
if (!in_array($task, ['user.login', 'reset.confirm'], true))
{
return;
}
// Login posts "username", the reset confirm form posts "jform[username]"
$jform = $input->get('jform', [], 'array');
$login = $task === 'user.login'
? $input->post->getString('username')
: (string) ($jform['username'] ?? '');
$login = trim($login);
if (!filter_var($login, FILTER_VALIDATE_EMAIL))
{
return;
}
$db = Factory::getContainer()->get(DatabaseInterface::class);
// Someone's username is literally this email, so leave it alone
$query = $db->createQuery()
->select('COUNT(*)')
->from($db->quoteName('#__users'))
->where($db->quoteName('username') . ' = :login')
->bind(':login', $login);
if ((int) $db->setQuery($query)->loadResult() > 0)
{
return;
}
// Find the username that owns this email
$query = $db->createQuery()
->select($db->quoteName('username'))
->from($db->quoteName('#__users'))
->where($db->quoteName('email') . ' = :email')
->bind(':email', $login);
$usernames = $db->setQuery($query, 0, 2)->loadColumn();
// Swap only when exactly one account matches
if (count($usernames) !== 1)
{
return;
}
if ($task === 'user.login')
{
$input->post->set('username', $usernames[0]);
}
else
{
$jform['username'] = $usernames[0];
$input->set('jform', $jform);
}
A few things worth knowing:
- Usernames still work. The snippet only acts when the value is a valid email address.
- A wrong email or password shows Joomla's usual error, so the form doesn't reveal which emails have an account.
- If an account's username is itself an email address, that account comes first and the snippet leaves the value alone.
- If 2 accounts share the same email, the snippet doesn't guess. It leaves the value alone.
- It works with both the login page and the Login module.
How to add this snippet
- Install Code Snippets if it isn't installed already.
- Log into your Joomla backend.
- Go to Components → Tassos Code Snippets.
- Click New.
- Enter a title, for example Login with email.
- Set Code Type to PHP.
- Set Insertion Method to Auto-Insert Locations.
- Set Location to On Page Load.
- Paste the code above into the editor.
- Click Save & Close.
Leave the Conditional Logic tab empty. The snippet checks the request itself and stops right away on every other page.
Change the "Username" label
The login form still says Username, so tell your users they can type their email too. Joomla's language overrides change that text without touching any files. You need 3 of them: one for the login page, one for the Login module, and one for the password reset step.
- Log into your Joomla backend.
- Go to System → Manage → Language Overrides.
- In the language filter, select your site language, for example English (en-GB) - Site.
- Click New.
- Enter
COM_USERS_LOGIN_USERNAME_LABELin Language Constant. - Enter
Username or Emailin Text. - Click Save & New.
- Repeat steps 5 to 7 for
MOD_LOGIN_VALUE_USERNAMEandCOM_USERS_FIELD_RESET_CONFIRM_USERNAME_LABEL.
That's it. Your users can now log in with their username or their email address, and you didn't have to install another plugin.
To send each user group to its own page after login, see Redirect Users After Login by User Group in Joomla. The 2 snippets work together. To learn more about how PHP snippets work, read the PHP snippet docs.