This section explains how to use PHP input in Conditions and Actions.
In this simple example we'll display a message on the front page every Monday. To do that, we'll use a PHP input in our Condition to evaluate to
TRUE if we're currently on the front page of our site, and if it's Monday today.
PHP Filter module on Drupal's module list page and assign relevant permissions if necessary. Take extra care as to whom you assign these permissions to, as the PHP input may cause security concerns so you probably don't want everyone on your website to be able to use it.
Add a new rule configuration and set the Event to System | Drupal is initializing.
Add a new Condition, Data | Data comparison and set it to site:current-date.
In the DATA VALUE field set, click on Switch to data selection and enter
In the PHP EVALUATION field, enter the Code value, as shown in the following screenshot, and save the Condition:
Add a new Condition and set the handler to PHP | Execute custom PHP code and enter this code in the text area:
Add a new Action, System | Show a message on the site and enter the following Value, as shown in the screenshot:
In the first Condition, we compare the current date value to figure out what day it is today. In the PHP Evaluation field, we always receive the value of the selected field in the
$value variable, which in this case is a timestamp of the current date. We're using this value in Drupal's
format_date() function to return
TRUE if it's Monday today.
In the second Condition, we're returning
TRUE if the current page we're visiting is the front page of our website.
PHP can be put to use in many other ways too. Some are described as follows:
We can also use PHP in Actions to execute functions, update database entries, and perform other tasks as required. To do that we can add an Action, Execute custom PHP code, and enter the PHP code we want to execute.
Using a PHP input in Rules is a very effective way to create custom Conditions and Actions if we don't want to programmatically create new ones in our custom module (more on that in the Providing new Events, Conditions, and Actions (Become an expert) recipe in this book). However, there are a number of things we want to keep in mind:
It is highly advised that we don't let regular users use the PHP input filter, as it is a high security risk.
Never use delimiters
We should never use the
<?php ?>delimiters in our custom code. Rules takes care of that for us. If we use the delimiters in our Condition or Action, it won't work.
Always test on a development site
Of course, it is advised that all Rules configurations are tested on a development site before using them on production sites. This is particularly valid for configurations that include the PHP code in Conditions or Actions. We always want to make sure we enter code without typos, execute the right database commands, or update the right user information.
It is also advised that Debugging is turned on on our development site, that way we can save a lot of time testing our configuration.