1. Home
  2. Docs
  3. Rules Developer Guide
  4. Extending Rules
  5. Conditions

Conditions

Registering a new condition allows it to be added to rules as a criteria.

Conditions are callback functions that return a true or false value depending on their input parameters. You can register existing functions from your plugin as a rules condition, or create new ones on the fly as part of your condition definition.

When you register a new condition, make sure that you prefix your condition key with a unique slug to avoid potential collisions with other plugins that register similar condition keys.

rules_register_condition( $key, $definition );

/**
 * @param  string          (required)   $key         A unique key that identifies the condition
 * @param  array|callable  (required)   $definition  The condition definition
 */

For a detailed specification of the $definition, see the Definitions page.

@Example Code

/* Number Comparison */
rules_register_condition( 'rules_number_comparison', array(
    'title' => 'Compare Numbers',
    'description' => 'Check the value of a number against another.',
    'configuration' => array(
        'form' => function( $form, $values, $condition ) {
            $compare_options = array(
                '>'     => 'Number 1 is greater than Number 2',
                '<'     => 'Number 1 is less than Number 2',
                '=='    => 'Number 1 is equal to Number 2',
                '!='    => 'Number 1 is not equal to Number 2',
                '>='    => 'Number 1 is greater than or equal to Number 2',
                '<='    => 'Number 1 is less than or equal to Number 2'
            );

            $form->addField( 'rules_comparison_type', 'choice', array( 
                'label' => __( 'Comparison Type', 'mwp-rules' ),
                'choices' => array_flip( $compare_options ),
                'expanded' => true,
                'required' => true,
                'data' => isset( $values['rules_comparison_type'] ) ? $values['rules_comparison_type'] : '>',
            ));                     
        },
    ),
    'arguments' => array(
        'number1' => array(
            'label' => 'Number 1',
            'argtypes' => array(
                'int'   => array( 'description' => 'a value to use as number 1' ),
                'float' => array( 'description' => 'a value to use as number 1' ),
            ),              
            'required'  => true,
            'configuration' => array(
                'form' => function( $form, $values, $condition ) {
                    $form->addField( 'rules_comparison_number1', 'text', array(
                        'label' => __( 'Number 1', 'mwp-rules' ),
                        'data' => isset( $values['rules_comparison_number1'] ) ? $values['rules_comparison_number1'] : '',
                    ));
                },
                'saveValues' => function( &$values, $condition ) {
                    settype( $values['rules_comparison_number1'], 'float' ); 
                },
                'getArg' => function( $values ) {
                    return $values['rules_comparison_number1'];
                },
            ),
        ),
        'number2' => array(
            'label' => 'Number 2',
            'default' => 'manual',
            'argtypes' => array(
                'int'   => array( 'description' => 'a value to use as number 2' ),
                'float' => array( 'description' => 'a value to use as number 2' ),
            ),              
            'required'  => true,
            'configuration' => array(
                'form' => function( $form, $values, $condition ) {
                    $form->addField( 'rules_comparison_number2', 'text', array(
                        'label' => __( 'Number 2', 'mwp-rules' ),
                        'data' => isset( $values['rules_comparison_number2'] ) ? $values['rules_comparison_number2'] : '',
                    ));
                },
                'saveValues' => function( &$values, $condition ) {
                    settype( $values['rules_comparison_number2'], 'float' ); 
                },
                'getArg' => function( $values ) {
                    return $values['rules_comparison_number2'];
                },
            ),
        ),
    ),              
    'callback' => function( $number1, $number2, $values ) {
        switch( $values[ 'rules_comparison_type' ] ) {
            case '<':  return $number1 < $number2;
            case '>':  return $number1 > $number2;
            case '==': return $number1 == $number2;
            case '!=': return $number1 != $number2;
            case '>=': return $number1 >= $number2;
            case '<=': return $number1 <= $number2;
            default: return FALSE;
        }
    },
));
Was this article helpful to you? Yes No

How can we help?