Conditions is a new and powerful feature in Oxygen using which we can control the output of elements conditionally.

Official documentation page: https://oxygenbuilder.com/documentation/other/conditions/

In this developer-centric article, I am going to share a summary of code for creating custom conditions from the above link with my own notes, comments and possible improvements regarding code formatting etc.

Let’s take the example of creating a custom condition called Current Post ID.

Using this condition user should be able to control the output of selected element(s) based on ID of a Post that is either entered manually or coming from a custom field.

Ex.: In the Template that applies to all single posts, output a Section on the front-end only for a Post whose ID is 104.

Here’s the code we need to add for this either using Code Snippets or a custom functionality plugin:

Version 1

<?php

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

	oxygen_vsb_register_condition(
		// Condition Name
		'Current Post ID',

		// Values: The array of pre-set values the user can choose from.
		// Set the custom key's value to true to allow users to input custom values.
		array( 
			'options' => array(),
			'custom' => true
		),

		// Operators
		array( '==', '!=', '>=', '<=', '>', '<' ),
		
		// Callback Function: Name of function that will be used to handle the condition
		'ex_condition_post_id_callback',

		// Condition Category: Default ones are Archive, Author, Other, Post, User
		'Post'
	);

}

/**
 * Callback function to handle the condition.
 * @param  mixed 	$value    	Input value - in this case, ID of a Post entered by the user.
 * @param  string 	$operator 	Comparison operator selected by the user.
 *
 * @return boolean 				true or false.
 */
function ex_condition_post_id_callback( $value, $operator ) {

	$current_post_id = get_the_ID();
	
	// return the integer value of $value
	$value = intval( $value );

	if ( $operator == "==" ) {
		if ( $current_post_id == $value ) {
			return true;
		} else {
			return false;
		}
	} else if ( $operator == "!=" ) {
		if ( $current_post_id != $value ) {
			return true;
		} else {
			return false;
		}
	} else if ( $operator == ">=" ) {
		if ( $current_post_id >= $value ) {
			return true;
		} else {
			return false;
		}
	} else if ( $operator == "<=" ) {
		if ( $current_post_id <= $value ) {
			return true;
		} else {
			return false;
		}
	} else if ( $operator == ">" ) {
		if ( $current_post_id > $value ) {
			return true;
		} else {
			return false;
		}
	} else if ( $operator == "<" ) {
		if ( $current_post_id < $value ) {
			return true;
		} else {
			return false;
		}
	}

}

Version 2

Let’s simplify the inner if conditions using the handy ternary operator.

<?php

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

	oxygen_vsb_register_condition(
		// Condition Name
		'Current Post ID',

		// Values: The array of pre-set values the user can choose from.
		// Set the custom key's value to true to allow users to input custom values.
		array( 
			'options' => array(),
			'custom' => true
		),

		// Operators
		array( '==', '!=', '>=', '<=', '>', '<' ),
		
		// Callback Function: Name of function that will be used to handle the condition
		'ex_condition_post_id_callback',

		// Condition Category: Default ones are Archive, Author, Other, Post, User
		'Post'
	);

}

/**
 * Callback function to handle the condition.
 * @param  mixed 	$value    	Input value - in this case, ID of a Post entered by the user.
 * @param  string 	$operator 	Comparison operator selected by the user.
 *
 * @return boolean 				true or false.
 */
function ex_condition_post_id_callback( $value, $operator ) {

	$current_post_id = get_the_ID();
	
	// return the integer value of $value
	$value = intval( $value );

	if ( $operator == "==" ) {
		return ( $current_post_id == $value ) ? true : false;
	} else if ( $operator == "!=" ) {
		return ( $current_post_id != $value ) ? true : false;
	} else if ( $operator == ">=" ) {
		return ( $current_post_id >= $value ) ? true : false;
	} else if ( $operator == "<=" ) {
		return ( $current_post_id <= $value ) ? true : false;
	} else if ( $operator == ">" ) {
		return ( $current_post_id > $value ) ? true : false;
	} else if ( $operator == "<" ) {
		return ( $current_post_id < $value ) ? true : false;
	}

}

Version 3

Oxygen provides a helper function called oxy_condition_eval_int that takes the value we are comparing to, selected value and operator and evaluates the inner if conditions.

i.e.,

if ( $operator == "==" ) {
	return ( $current_post_id == $value ) ? true : false;
} else if ( $operator == "!=" ) {
	return ( $current_post_id != $value ) ? true : false;
} else if ( $operator == ">=" ) {
	return ( $current_post_id >= $value ) ? true : false;
} else if ( $operator == "<=" ) {
	return ( $current_post_id <= $value ) ? true : false;
} else if ( $operator == ">" ) {
	return ( $current_post_id > $value ) ? true : false;
} else if ( $operator == "<" ) {
	return ( $current_post_id < $value ) ? true : false;
}

can be simplified as

return oxy_condition_eval_int( $current_post_id, $value, $operator );

so our full code then becomes:

<?php

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

	oxygen_vsb_register_condition(
		// Condition Name
		'Current Post ID',

		// Values: The array of pre-set values the user can choose from.
		// Set the custom key's value to true to allow users to input custom values.
		array( 
			'options' => array(),
			'custom' => true
		),

		// Operators
		array( '==', '!=', '>=', '<=', '>', '<' ),
		
		// Callback Function: Name of function that will be used to handle the condition
		'ex_condition_post_id_callback',

		// Condition Category: Default ones are Archive, Author, Other, Post, User
		'Post'
	);

}

/**
 * Callback function to handle the condition.
 * @param  mixed 	$value    	Input value - in this case, ID of a Post entered by the user.
 * @param  string 	$operator 	Comparison operator selected by the user.
 *
 * @return boolean 				true or false.
 */
function ex_condition_post_id_callback( $value, $operator ) {

	$current_post_id = get_the_ID();
	
	// return the integer value of $value
	$value = intval( $value );

	return oxy_condition_eval_int( $current_post_id, $value, $operator );

}

If we were evaluating a string instead of int, use oxy_condition_eval_string() instead similarly.

Version 4

array( '==', '!=', '>=', '<=', '>', '<' )

can be replaced with

$oxy_condition_operators['int']

which is a global variable that Oxygen adds.

So our final code then becomes:

<?php

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

	global $oxy_condition_operators;

	oxygen_vsb_register_condition(
		// Condition Name
		'Current Post ID',

		// Values: The array of pre-set values the user can choose from.
		// Set the custom key's value to true to allow users to input custom values.
		array( 
			'options' => array(),
			'custom' => true
		),

		// Operators
		$oxy_condition_operators['int'],
		
		// Callback Function: Name of function that will be used to handle the condition
		'ex_condition_post_id_callback',

		// Condition Category: Default ones are Archive, Author, Other, Post, User
		'Post'
	);

}

/**
 * Callback function to handle the condition.
 * @param  mixed 	$value    	Input value - in this case, ID of a Post entered by the user.
 * @param  string 	$operator 	Comparison operator selected by the user.
 *
 * @return boolean 				true or false.
 */
function ex_condition_post_id_callback( $value, $operator ) {

	$current_post_id = get_the_ID();
	
	// return the integer value of $value
	$value = intval( $value );

	return oxy_condition_eval_int( $current_post_id, $value, $operator );

}

Note:

$oxy_condition_operators['int'] = array( '==', '!=', '>=', '<=', '>', '<' )
$oxy_condition_operators['string'] = array( '==', '!=', 'contains', 'does not contain' )
$oxy_condition_operators['simple'] = array( '==', '!=' )

Screencasts on Conditions

References

https://wpaustralia.slack.com/archives/C054S58MK/p1562317221304500

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE
magnifiercross
>
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
%d bloggers like this: