Voxel Mods: Randomize Output

The Randomize Output Modifier for the Voxel WordPress Theme allows you to input a comma-separated list, such as a list of image Gallery IDs or taxonomies and outputs a number of random values each time the page is refreshed.

Whether you want to showcase a random image from a gallery or display a different taxonomy term on each visit, this modifier makes it easy to add variety to your site’s dynamic content.

In addition to the comma-separated list, you can provide additional input options, which are seamlessly integrated into the pool of potential outputs. This makes it easy to expand your choices without altering the original list, giving you greater control over the randomness while ensuring that the content remains fresh and engaging for your visitors.

Use Case

Displaying a random image

When applied to an Image widget, this modifier accepts a list of gallery IDs along with additional file IDs or dynamic tags. It then randomly selects a single image ID from the list to be displayed. The selected image changes with each page refresh, adding a dynamic touch to the content.

(additional options 1 and 2 are file IDs from the Media Libary)

Changing text output

When applied to a Text widget, this modifier randomly outputs either the original text or one of the additional options or dynamic tags that have been provided.

The example below is not ideal as the text output is static, but you could have a repeater of text options or a list of keywords, from which one is chosen at random. In that case, you would not add any additional options and the Mod would output one value from the given list.

Security implementations

The core of the modifier relies on the sanitization of input values to ensure that the data remains safe and clean for use in WordPress. The comma-separated list is sanitized using sanitize_text_field(), which ensures that any unwanted characters or HTML tags are removed before being processed. Additionally, the modifier handles the additional input fields, ensuring that any data added by users is properly sanitized and validated before being incorporated into the randomized list. To further prevent XSS (Cross-Site Scripting) vulnerabilities, the selected value is passed through esc_html() before being outputted to the page, ensuring that any content displayed is properly escaped.

Conclusion

In conclusion, the Randomize Output Modifier for Voxel offers a simple yet powerful solution for introducing dynamic, randomized content into your WordPress site.

By accepting comma-separated lists and additional input options, this modifier provides flexibility and ease of use, allowing you to add engaging features like rotating gallery images, random taxonomies, or diverse content displays with minimal effort.

Coupled with robust security measures such as input sanitization and output escaping, this tool ensures a safe and smooth user experience.

Whether you’re looking to spice up your site’s content or provide a more interactive experience for your visitors, the Randomize Output Modifier is an invaluable addition to your Voxel toolkit.

Unleash Dynamic Data with Voxel!

With Voxel’s design flexibility, the possibilities are endless. Get Voxel here!

The Code: Randomize Output Mod

v2 – The code was updated to allow multiple outputs.

*** Disclaimer – Use at your own risk. Always make a backup. ***

Add to your child theme’s functions.php file.

add_filter( 'voxel/dynamic-data/modifiers', function( $modifiers ) {
    class Randomize_Output_Modifier extends \Voxel\Dynamic_Data\Modifiers\Base_Modifier {
        public function get_label(): string {
            return 'Randomize Output';
        }

        public function get_key(): string {
            return 'randomize_output';
        }

        protected function define_args(): void {
            // Define number of outputs
            $this->define_arg([
                'type' => 'number',
                'label' => _x( 'Number of Outputs', 'modifiers', 'voxel-backend' ),
                'default' => 1,
            ]);
            
            // Define up to 6 additional inputs
            for ( $i = 1; $i <= 6; $i++ ) {
                $this->define_arg([
                    'type' => 'text',
                    'label' => _x( "Additional Option $i", 'modifiers', 'voxel-backend' ),
                    'default' => null,
                ]);
            }
        }

        public function apply( string $value ) {
            // Sanitize the input value and split it into an array
            $sanitized_value = sanitize_text_field( $value );
            $options = array_filter( array_map( 'trim', explode( ',', $sanitized_value ) ) );

            // Add additional inputs to the list if provided
            for ( $i = 1; $i <= 6; $i++ ) {
                $input = sanitize_text_field( $this->get_arg( $i ) );
                if ( ! empty( $input ) ) {
                    $options[] = $input;
                }
            }

            // Get the user-defined number of outputs
            $num_outputs = (int) $this->get_arg(0);
            $num_outputs = max(1, min(count($options), $num_outputs)); // Ensure valid range
            
            // If there are valid options, randomize and return the selected ones
            if ( ! empty( $options ) ) {
                shuffle( $options );
                $selected_options = array_slice( $options, 0, $num_outputs );
                return esc_html( implode(', ', $selected_options) );
            }

            // If no valid options are provided, return the original value
            return esc_html( $value );
        }
    }

    $modifiers['randomize_output'] = \Randomize_Output_Modifier::class;
    return $modifiers;
} );

To implement this mod, you’ll need to add the code to your WordPress site. You have two options for where to place this code:

  • Child Theme’s functions.php File:

    If you’re already using a child theme (recommended for customization), simply add the PHP code to your functions.php file. This will ensure your changes are preserved even if the main Voxel theme is updated.

  • Code Snippets Plugin:

    If you prefer not to edit theme files directly, or if you want to manage your custom code more easily, you can use the Fluent Snippets plugin. This plugin allows you to add custom PHP code without touching your theme files. Just create a new function.php snippet, paste the code, and activate it.

Then apply this Voxel Mod from the Elementor builder interface.

More Articles

Taxonomies are used to group and organize content. Custom fields are a way to add different types of data to your content. Custom post types, taxonomies and custom fields offer your content management system complete dynamic data. What are Taxonomies? Taxonomies are the collective name for grouping terms, like the WordPress default categories or tags, […]
This modifier enables you to trim the content of a Gutenberg WordPress post by specifying a range of heading blocks. Within the defined range, it also includes any non-heading content such as paragraphs, images, and buttons. The code counts the Core/Heading blocks from a Gutenberg post, Generateblocks/Headline from GenerateBlocks, and Kadence/Advancedheading from Kadence Blocks. Other […]

Support me

Coffee is my fuel

All guides are available for FREE.

If this saved you time, please support my work by checking out the recommended tools or buying me a coffee.