Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Created June 25, 2025 06:13
Show Gist options
  • Save mehrshaddarzi/d378a4f46ddca0a899422e9bfd754bce to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/d378a4f46ddca0a899422e9bfd754bce to your computer and use it in GitHub Desktop.
New controller in elementor with php
<?php
// document
https://developers.elementor.com/docs/controls/
// Define
use Elementor\Base_Data_Control;
class My_Select2_Control extends Base_Data_Control {
public function get_type() {
return 'my_select2_control';
}
public function enqueue() {
// بارگذاری کتابخانه Select2 و CSS مربوطه (از نسخه المنتور استفاده می‌کنیم)
wp_enqueue_style( 'select2' );
wp_enqueue_script( 'select2' );
}
public function get_default_settings() {
return [
'options' => [], // آرایه کلید=>مقدار برای سلکت
'multiple' => false,
'placeholder' => '',
];
}
protected function content_template() {
?>
<#
var multiple = settings.multiple ? 'multiple' : '';
var placeholder = settings.placeholder || '';
#>
<select class="my-select2-control" style="width: 100%;" {{{ multiple }}} data-placeholder="{{ placeholder }}">
<# _.each( settings.options, function( label, key ) { #>
<option value="{{ key }}" <# if ( settings.value == key ) { #>selected="selected"<# } #>>{{{ label }}}</option>
<# }); #>
</select>
<script>
jQuery( document ).ready( function( $ ) {
var $select = $( '.my-select2-control' );
if ( $select.length && ! $select.hasClass( 'select2-hidden-accessible' ) ) {
$select.select2({
placeholder: $select.data('placeholder'),
allowClear: true,
width: '100%'
});
// بروزرسانی مقدار المنتور هنگام تغییر انتخاب
$select.on( 'change', function() {
var val = $(this).val();
elementor.channels.editor.trigger('change', {
id: '{{ data.controlUid }}',
value: val
});
});
}
});
</script>
<?php
}
}
// Register
add_action( 'elementor/controls/register', function( $controls_manager ) {
require_once( 'path-to/My_Custom_Control.php' );
$controls_manager->register_control( 'my_custom_control', new My_Custom_Control() );
} );
// Using in widget
$this->add_control(
'my_field',
[
'label' => 'کنترل سفارشی من',
'type' => 'my_custom_control', // همان نامی که در get_type() تعریف کردید
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment