phpwordpresselementorelement-ordering

How to prevent sorting of options in a SELECT control of Elementor Addon


I came across one issue with the Elementor while checking for the custom select dropdown Below is my code, I am using with the custom widget

Value of key in a option are number or integer and not string

<?php
$this->add_control( 'Legacy', [
    'type' => \Elementor\Controls_Manager::SELECT,
    'label' => 'Legacy',
    'options' => array_replace([
        '20' => esc_html__( 'Linear', 'elementor' ),
        '10' => esc_html__( 'Radial', 'elementor' ),
    ]),
] );
?>

The expected result in a select dropdown should be

Linear
Radial

Right?

But, when I see the result, it says

Radial
Linear

What I think is Elementor is filtering the options by key automatically before rendering

I am unable to find any filters or hooks associated with this

Do any of you have a solution to prevent this filtering of options and display as passed in the 'options' argument?


Solution

  • Elementor’s SELECT control auto sort option by their keys before rendering. Use empty array [] to preserve original order

    <?php
    
    $this->add_control( 'Legacy', [
        'type' => \Elementor\Controls_Manager::SELECT,
        'label' => 'Legacy',
        'options' => array_replace(
            [], // Placeholder to preserve the original order
            [
                '20' => esc_html__( 'Linear', 'elementor' ),
                '10' => esc_html__( 'Radial', 'elementor' ),
            ]
        ),
    ] );
    
    ?>