This site is running Voxel Theme Version: 1.6.1.2 | Elementor Plugin Version: 3.31.2

Join the Voxel Guide Community!

Get Involved

Dynamic Tag Math operators

Description/Instructions

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:

  • Sum two numbers: @post(number-5).math_op(+, @post(number-4))
  • Get 15% of a value: @post(price).math_op(percent_of, 15)
  • Increase by percent (markup): @post(price).math_op(increase_percent, 12.5)
  • Decrease by percent (discount) and format: @post(price).math_op(decrease_percent, @post(discount)).number_format(2)

Instructions

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 );
}

  • PHP
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24