Get Involved
No items added to cart
No items added to cart
Adds a custom Voxel Dynamic Data modifier “Math operation” (math_op) to the modifier list, enabling basic arithmetic (+, -, ×, ÷) and percentage operations (percent of, increase/decrease by %) with dynamic operands. Use in dynamic tags like: @post(number-5).math_op(+, @post(number-4)).
Usage examples:
Create a new php snippet, save and enable. Hard refresh editor to ensure math mod loads correctly
// Register the modifier EARLY so it's in Voxel's config before it's cached.
add_filter( 'voxel/dynamic-data/modifiers', function( $mods ) {
$mods['math_op'] = 'Voxel_Custom_Math_Modifier';
return $mods;
}, 0 );// Define the class when the base class is available (now or after theme setup).
if ( class_exists( '\Voxel\Dynamic_Data\Modifiers\Base_Modifier' ) && ! class_exists( 'Voxel_Custom_Math_Modifier' ) ) {
class Voxel_Custom_Math_Modifier extends \Voxel\Dynamic_Data\Modifiers\Base_Modifier {
public function get_label(): string { return _x( 'Math operation', 'modifiers', 'voxel-backend' ); }
public function get_key(): string { return 'math_op'; }
public function get_description(): string { return 'Perform +, -, ×, ÷, percent-of, increase-by-% or decrease-by-%.'; }
public function expects(): array { return [ static::TYPE_NUMBER ]; }protected function define_args(): void {
$this->define_arg( [
'type' => 'select',
'label' => _x( 'Operator', 'modifiers', 'voxel-backend' ),
'choices' => [
'+' => 'Add (+)',
'-' => 'Subtract (-)',
'*' => 'Multiply (×)',
'/' => 'Divide (÷)',
'percent_of' => 'Percent of (%)',
'increase_percent' => 'Increase by % (+%)',
'decrease_percent' => 'Decrease by % (-%)',
],
] );$this->define_arg( [
'type' => 'text',
'label' => _x( 'Operand', 'modifiers', 'voxel-backend' ),
] );
}public function apply( string $value ) {
if ( ! is_numeric( $value ) ) { return $value; }
$op = $this->get_arg(0);
$operand = $this->get_arg(1);
if ( ! is_numeric( $operand ) ) { $operand = 0; }
$a = (float) $value; $b = (float) $operand;switch ( $op ) {
case '+': return $a + $b;
case '-': return $a - $b;
case '*': return $a * $b;
case '/': return ($b == 0.0) ? $value : ($a / $b);
case 'percent_of': return $a * ($b / 100);
case 'increase_percent': return $a * (1 + ($b / 100));
case 'decrease_percent': return $a * (1 - ($b / 100));
default: return $value;
}
}
}
} else {
add_action( 'after_setup_theme', function() {
if ( class_exists( '\Voxel\Dynamic_Data\Modifiers\Base_Modifier' ) && ! class_exists( 'Voxel_Custom_Math_Modifier' ) ) {
class Voxel_Custom_Math_Modifier extends \Voxel\Dynamic_Data\Modifiers\Base_Modifier {
public function get_label(): string { return _x( 'Math operation', 'modifiers', 'voxel-backend' ); }
public function get_key(): string { return 'math_op'; }
public function get_description(): string { return 'Perform +, -, ×, ÷, percent-of, increase-by-% or decrease-by-%.'; }
public function expects(): array { return [ static::TYPE_NUMBER ]; }
protected function define_args(): void {
$this->define_arg( [
'type' => 'select',
'label' => _x( 'Operator', 'modifiers', 'voxel-backend' ),
'choices' => [
'+' => 'Add (+)',
'-' => 'Subtract (-)',
'*' => 'Multiply (×)',
'/' => 'Divide (÷)',
'percent_of' => 'Percent of (%)',
'increase_percent' => 'Increase by % (+%)',
'decrease_percent' => 'Decrease by % (-%)',
],
] );
$this->define_arg( [
'type' => 'text',
'label' => _x( 'Operand', 'modifiers', 'voxel-backend' ),
] );
}
public function apply( string $value ) {
if ( ! is_numeric( $value ) ) { return $value; }
$op = $this->get_arg(0);
$operand = $this->get_arg(1);
if ( ! is_numeric( $operand ) ) { $operand = 0; }
$a = (float) $value; $b = (float) $operand;switch ( $op ) {
case '+': return $a + $b;
case '-': return $a - $b;
case '*': return $a * $b;
case '/': return ($b == 0.0) ? $value : ($a / $b);
case 'percent_of': return $a * ($b / 100);
case 'increase_percent': return $a * (1 + ($b / 100));
case 'decrease_percent': return $a * (1 - ($b / 100));
default: return $value;
}
}
}
}
}, 0 );
}
Are you sure you want to exit? Your current conversation will be lost.