Skip to content

Instantly share code, notes, and snippets.

@ochoarobert1
Last active February 6, 2024 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ochoarobert1/b4e25f1acc258986518cbcefdf4bcf0c to your computer and use it in GitHub Desktop.
Save ochoarobert1/b4e25f1acc258986518cbcefdf4bcf0c to your computer and use it in GitHub Desktop.
Dinamically add all Elementor widgets inside hello elementor child theme
<?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