Last active
February 6, 2024 14:11
-
-
Save ochoarobert1/b4e25f1acc258986518cbcefdf4bcf0c to your computer and use it in GitHub Desktop.
Dinamically add all Elementor widgets inside hello elementor child theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CustomElementorWidgets | |
{ | |
public function __construct() | |
{ | |
add_action('wp_enqueue_scripts', [$this, 'widgets_dependencies'], 80); | |
add_action('elementor/widgets/register', [$this, 'widgets_register']); | |
} | |
function widgets_dependencies() | |
{ | |
$folders = $this->get_widget_folders(); | |
if (count($folders) < 1) return; | |
foreach ($folders as $folder) { | |
wp_register_style($folder . '-style', get_stylesheet_directory_uri() . '/assets/css/elementor/' . $folder . '.css', [], HELLO_ELEMENTOR_CHILD_VERSION, 'all'); /* wp_register_script( $folder . '-script' , get_stylesheet_directory_uri() . '/assets/js/elementor/' . $folder . '.js' , ['jquery'], HELLO_ELEMENTOR_CHILD_VERSION, true ); */ | |
} | |
} | |
public function widgets_register($widgets_manager) | |
{ | |
$path = get_stylesheet_directory() . '/widgets'; | |
$folders = $this->get_widget_folders(); | |
if (count($folders) < 1) return; | |
foreach ($folders as $folder) { | |
$className = '\\' . preg_replace('/\s+/', '', ucwords(preg_replace('/[-]/', ' ', $folder))); | |
require_once $path . '/' . $folder . '/widget.php'; | |
$widgets_manager->register(new $className()); | |
} | |
} | |
public function get_widget_folders() | |
{ | |
$path = get_stylesheet_directory() . '/widgets'; | |
$folders = scandir($path); | |
unset($folders[array_search('.', $folders, true)]); | |
unset($folders[array_search('..', $folders, true)]); | |
return $folders; | |
} | |
} | |
new CustomElementorWidgets; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment