ACF PHP - Run PHP code in a Joomla Field

The PHP custom field gives you the ability to run any PHP code you desire within an Article.

How to add a PHP Custom Field to Joomla! Articles

The PHP field provides you the way to set the default PHP code to run when you view an Article.

acf-php-field-settings

Let's see what each option does.

Name Description
Name The name will be used to identify the field. Leave this blank and Joomla will fill in a default value from the title.
Label The label of the field to display.
Description A description of the field that will be displayed in the label tooltip.
Required Is this a mandatory field?
Default Value Set the default PHP code

Blocking specific PHP functions

The PHP field allows you to create a list of forbidden php functions. If any of these functions are found within the php code, then the whole php block of code will not be executed.

To edit the list, you can do so by going on Extensions > Plugins > Fields - ACF PHP > Forbidden PHP Functions. You can see the list of the forbidden php functions below.

acf-php-forbidden-php-functions

In the textarea, you enter a comma separated list of functions.

Usage

Once you are in your Article's Edit screen, choose the "Fields" Tab to see the PHP custom field as you can see in the screenshot below.

acf-php-field-usage

In the textarea, you enter your own PHP code to run.

Frontend Display

Take a look at the screenshot below to see how it could be displayed in your frontend.

acf-php-frontend

Examples

Below you can find a list of PHP code examples.

Simple message output

return 'Hello World!';

Variable usage

$name = 'John Doe';
return 'My name is: ' . $name;

Accessing the user object

return 'User is ' . (($user->guest) ? 'a guest' : 'logged in');

Accessing the field object

return 'The title of the field is: ' . $field->title;

Accessing item's data

We call "Item" the element where the field is attached to. In most cases the "Item" is the article where the field is being rendered.

return 'The article title is : ' . $item->title;

Frequently Asked Questions

How can I override the layout of "ACF - PHP" custom field?

To override the "ACF - PHP" custom field, read here: How to override the "ACF - PHP" custom field.
Last updated on Nov 26th 2024 12:11