PHP inside a Sourcerer block can use PHP itself and the APIs made available by Joomla. This lets trusted users work with session and user data, query the database, and generate dynamic output.
PHP code can read or change site data, so only allow it for users and content areas you trust. See Security Control for the relevant settings.
PHP syntax in Sourcerer
Place PHP code inside PHP tags within the Sourcerer tags:
{source}<?php
echo 'Hello';
?>{/source}
Sourcerer also accepts the shorthand forms <? ... ?> and <?= ... ?>. It normalizes them to <?php ... ?> and <?php echo ... ?> before execution, so this does not depend on PHP's short_open_tag setting.
Using namespaced classes
You can place a simple namespace import on its own line inside a Sourcerer block. Sourcerer moves that use statement to valid file scope before executing the code:
{source}<?php
use Joomla\Database\DatabaseFactory;
$factory = new DatabaseFactory();
?>{/source}
You can also use a fully qualified class name, such as new \Joomla\Database\DatabaseFactory(), without an import.
Reusing variables
Variables you create in one Sourcerer PHP block are available to Sourcerer PHP blocks processed later in the same request. They do not persist into a later page request.
Ready-to-use PHP variables and objects
Sourcerer makes these commonly used Joomla objects and values available without requiring you to initialize them:
$app: the Joomla application$documentor$doc: the current document$databaseor$db: the Joomla site database connection$user: the guest or currently logged-in user$Itemid: the active menu item ID$article: the current article when Sourcerer has article context$module: the current module when Sourcerer has module context
Connecting to a database
Joomla's database
Use the ready-to-use $db object to query Joomla's database. Build queries with Joomla's query API and bind dynamic values instead of concatenating them into SQL:
{source}<?php
$value = 'that';
$query = $db->getQuery(true)
->select($db->quoteName('something'))
->from($db->quoteName('#__mydatabasetable'))
->where($db->quoteName('this') . ' = :value')
->bind(':value', $value);
$result = $db->setQuery($query)->loadResult();
// Do something with $result
?>{/source}
See the Joomla database documentation for query, result, and data-manipulation examples.
External database
Use Joomla's DatabaseFactory to create a separate connection. This MySQL example uses placeholder credentials and keeps the connection in $externalDb so the ready-to-use $db object remains Joomla's own connection:
{source}<?php
use Joomla\Database\DatabaseFactory;
$options = [
'host' => 'db.example.com',
'user' => 'example_user',
'password' => 'replace-with-secret',
'database' => 'example_database',
'prefix' => '',
];
$externalDb = (new DatabaseFactory())->getDriver('mysqli', $options);
// Use $externalDb to build and run queries on the external database
?>{/source}
Do not store real database credentials in article content that other users can view or edit. Load secrets from a protected configuration source instead.