Description:
I am developing Elementor widgets for a more efficient approach. Rather than creating separate classes for each widget, I prefer to use a single class to handle multiple widgets. To achieve this, I want to dynamically configure each widget’s properties and fields using an array structure.
Folder Structure
elementor-addon/
- widgets/widget.php
- elementor-addon.php
Code
<code>**elementor-addon.php**
<?php
/**
* Plugin Name: Elementor Addon
* Description: Simple hello world widgets for Elementor.
* Version: 1.0.0
* Author: Elementor Developer
* Author URI: https://developers.elementor.com/
* Text Domain: elementor-addon
*
* Requires Plugins: elementor
* Elementor tested up to: 3.21.0
* Elementor Pro tested up to: 3.21.0
*/
function register_hello_world_widget( $widgets_manager ) {
require_once( __DIR__ . '/widgets/widget.php' );
$widgets = [
[
'title' => 'Widget 1',
'id' => 'test_widget_1',
'icon' => 'eicon-code',
'categories' => ['basic'],
'fields' => [
[
'id' => 'title',
'name' => 'Title',
'type' => 'text',
'std' => 'Hello World'
],
[
'id' => 'subtitle',
'name' => 'Subtitle',
'type' => 'textarea',
'std' => 'Hello World'
]
]
],
[
'title' => 'Widget 2',
'id' => 'test_widget_2',
'icon' => 'eicon-code',
'categories' => ['basic'],
'fields' => [
[
'id' => 'title',
'name' => 'Title',
'type' => 'text',
'std' => 'Hello World'
],
[
'id' => 'subtitle',
'name' => 'Subtitle',
'type' => 'textarea',
'std' => 'Hello World'
]
]
]
];
foreach ($widgets as $widget) {
$widgets_manager->register( new My_Widget( $widget ) );
}
}
add_action( 'elementor/widgets/register', 'register_hello_world_widget' );
</code>
<code>**elementor-addon.php**
<?php
/**
* Plugin Name: Elementor Addon
* Description: Simple hello world widgets for Elementor.
* Version: 1.0.0
* Author: Elementor Developer
* Author URI: https://developers.elementor.com/
* Text Domain: elementor-addon
*
* Requires Plugins: elementor
* Elementor tested up to: 3.21.0
* Elementor Pro tested up to: 3.21.0
*/
function register_hello_world_widget( $widgets_manager ) {
require_once( __DIR__ . '/widgets/widget.php' );
$widgets = [
[
'title' => 'Widget 1',
'id' => 'test_widget_1',
'icon' => 'eicon-code',
'categories' => ['basic'],
'fields' => [
[
'id' => 'title',
'name' => 'Title',
'type' => 'text',
'std' => 'Hello World'
],
[
'id' => 'subtitle',
'name' => 'Subtitle',
'type' => 'textarea',
'std' => 'Hello World'
]
]
],
[
'title' => 'Widget 2',
'id' => 'test_widget_2',
'icon' => 'eicon-code',
'categories' => ['basic'],
'fields' => [
[
'id' => 'title',
'name' => 'Title',
'type' => 'text',
'std' => 'Hello World'
],
[
'id' => 'subtitle',
'name' => 'Subtitle',
'type' => 'textarea',
'std' => 'Hello World'
]
]
]
];
foreach ($widgets as $widget) {
$widgets_manager->register( new My_Widget( $widget ) );
}
}
add_action( 'elementor/widgets/register', 'register_hello_world_widget' );
</code>
**elementor-addon.php**
<?php
/**
* Plugin Name: Elementor Addon
* Description: Simple hello world widgets for Elementor.
* Version: 1.0.0
* Author: Elementor Developer
* Author URI: https://developers.elementor.com/
* Text Domain: elementor-addon
*
* Requires Plugins: elementor
* Elementor tested up to: 3.21.0
* Elementor Pro tested up to: 3.21.0
*/
function register_hello_world_widget( $widgets_manager ) {
require_once( __DIR__ . '/widgets/widget.php' );
$widgets = [
[
'title' => 'Widget 1',
'id' => 'test_widget_1',
'icon' => 'eicon-code',
'categories' => ['basic'],
'fields' => [
[
'id' => 'title',
'name' => 'Title',
'type' => 'text',
'std' => 'Hello World'
],
[
'id' => 'subtitle',
'name' => 'Subtitle',
'type' => 'textarea',
'std' => 'Hello World'
]
]
],
[
'title' => 'Widget 2',
'id' => 'test_widget_2',
'icon' => 'eicon-code',
'categories' => ['basic'],
'fields' => [
[
'id' => 'title',
'name' => 'Title',
'type' => 'text',
'std' => 'Hello World'
],
[
'id' => 'subtitle',
'name' => 'Subtitle',
'type' => 'textarea',
'std' => 'Hello World'
]
]
]
];
foreach ($widgets as $widget) {
$widgets_manager->register( new My_Widget( $widget ) );
}
}
add_action( 'elementor/widgets/register', 'register_hello_world_widget' );
<code>**widget.php**
<?php
class My_Widget extends ElementorWidget_Base {
public $config = [];
public function __construct($args) {
$this->config = $args;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function get_name() {
return $this->config['id'];
}
public function get_title() {
return $this->config['title'];
}
public function get_icon() {
return 'eicon-code';
}
public function get_categories() {
return [ 'basic' ];
}
public function get_keywords() {
return [ 'hello', 'world' ];
}
protected function render() {
?>
<p> Hello World </p>
<?php
}
protected function content_template() {
?>
<p> Hello World </p>
<?php
}
}
</code>
<code>**widget.php**
<?php
class My_Widget extends ElementorWidget_Base {
public $config = [];
public function __construct($args) {
$this->config = $args;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function get_name() {
return $this->config['id'];
}
public function get_title() {
return $this->config['title'];
}
public function get_icon() {
return 'eicon-code';
}
public function get_categories() {
return [ 'basic' ];
}
public function get_keywords() {
return [ 'hello', 'world' ];
}
protected function render() {
?>
<p> Hello World </p>
<?php
}
protected function content_template() {
?>
<p> Hello World </p>
<?php
}
}
</code>
**widget.php**
<?php
class My_Widget extends ElementorWidget_Base {
public $config = [];
public function __construct($args) {
$this->config = $args;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function get_name() {
return $this->config['id'];
}
public function get_title() {
return $this->config['title'];
}
public function get_icon() {
return 'eicon-code';
}
public function get_categories() {
return [ 'basic' ];
}
public function get_keywords() {
return [ 'hello', 'world' ];
}
protected function render() {
?>
<p> Hello World </p>
<?php
}
protected function content_template() {
?>
<p> Hello World </p>
<?php
}
}
when I click Update it shows ‘Server Error‘.
References
-
Creating Your First Addon
-
PHP Magic Methods Cheatsheet