Technical Library / Open Source

Joomla Snippets

Searchable collection of production-ready code recipes for template overrides, modules, plugins, and Joomla! 3 to 6 APIs.

100 Snippets
12 Categories
J3 to J6 Compatibility
Showing 10 of 10 snippets
PHP J6 J5 J3 Database
Database and Models

Run Secure SELECT Queries with Prepared Statements (bind and ParameterType)

Queries the database via DatabaseInterface, quoteName() and typed bound parameters to eliminate SQL injection.

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
// ...
75 255 2 versions
PHP J6 J5 J4 Database
Database and Models

Filter Multiple IDs Safely with whereIn() and ParameterType

Replaces unsafe implode() IN (...) clauses with native whereIn() automatic parameter binding.

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
// ...
43 154
PHP J6 J5 J3 Database
Database and Models

Insert a Database Record and Get Its Generated ID with insertObject()

Persists a stdClass directly into a table via $db->insertObject() and automatically populates its primary key property.

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;

// ...
41 146 2 versions
PHP J6 J5 J3 Database
Database and Models

Update Specific Columns Safely with Bound UPDATE Queries

Runs targeted SQL UPDATE statements (such as atomic counter increments or state changes) without overwriting other columns.

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
// ...
36 135 2 versions
PHP J6 J5 J3 Database
Database and Models

Run Atomic Operations with SQL Transactions (transactionStart and Rollback)

Wraps multi-table writes inside an InnoDB transaction with automatic rollback on failure to guarantee data integrity.

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;

// ...
34 128 2 versions
PHP J6 J5 J3 Database
Database and Models

Index Query Results by Key with loadObjectList() and loadAssocList()

Eliminates boilerplate re-indexing loops by passing the key column directly into loadObjectList('id') or loadAssocList('code', 'qty').

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;

// ...
29 111 2 versions
SQL J6 J5 J4 Database
Database and Models

Convert Legacy Joomla Tables to InnoDB and utf8mb4_unicode_ci

Generates exact ALTER TABLE statements from information_schema to migrate legacy utf8mb3/MyISAM tables cleanly to utf8mb4 and InnoDB.

-- Gera os comandos ALTER TABLE para todas as tabelas que ainda não estão em InnoDB + utf8mb4
SELECT CONCAT(
    'ALTER TABLE `', TABLE_NAME,
    '` ENGINE=InnoDB CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'
) AS ddl_statement
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
// ...
46 157
SQL J6 J5 J4 J3 Database
Database and Models

Audit the Largest Joomla Database Tables in Megabytes

Lists the top 20 largest tables (data + index size) to spot bloated tables such as #__session, #__history, #__redirect_links or logs.

SELECT
    TABLE_NAME AS tabela,
    ENGINE,
    TABLE_ROWS AS linhas_aprox,
    ROUND(DATA_LENGTH / 1024 / 1024, 2) AS dados_mb,
    ROUND(INDEX_LENGTH / 1024 / 1024, 2) AS indices_mb,
    ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) AS total_mb
// ...
34 123
SQL J6 J5 J4 Database
Database and Models

List Enabled Third-Party Extensions Directly from #__extensions

Queries all enabled non-core extensions (protected = 0) for fast pre-upgrade audits on Joomla 4, 5 and 6.

SELECT
    extension_id,
    type,
    folder,
    element,
    name,
    JSON_UNQUOTE(JSON_EXTRACT(manifest_cache, '$.version')) AS versao
// ...
43 151
SQL J6 J5 J4 Database
Database and Models

Prune Expired Sessions and Unlock Checked-Out Articles via SQL

Deletes stale sessions from #__session and clears abandoned article checkouts (checked_out) safely with NULL timestamps.

-- Remove sessões de visitantes com mais de 24 horas
DELETE FROM `#__session`
WHERE guest = 1
  AND time < UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY);

-- Desbloqueia artigos travados em edição há mais de 2 horas
UPDATE `#__content`
// ...
32 119
Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.