Register new widgets in functions.php
-
Hello,
I’m trying to register new Elementor Widget without creating a plugin. So I wish to register it in my functions.php
Regarding to Elementor developper’s guide, I need to use :
function register_new_widgets( $widgets_manager ) { require_once( __DIR__ . '/my-elementor-widget.php' ); $widgets_manager->register( new \widget_class_name() ); } add_action( 'elementor/widgets/register', 'register_new_widgets' );
Perhaps, I was having a fatal error saying my Class doesn’t exists. After a long search I discover that was because Elementor class hasn’t been loaded yet when registering this new classes and that I need to use Elementor Initialization to get my Widget loaded only when Elementor will load other widgets.
So regarding the second documentationc, I’ve made something similar as :
final class Plugin { /** * Initialize * * Load the addons functionality only after Elementor is initialized. * * Fired by <code>elementor/init</code> action hook. * * @since 1.0.0 * @access public */ public function init() { add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] ); } /** * Register Widgets * * Load widgets files and register new Elementor widgets. * * Fired by <code>elementor/widgets/register</code> action hook. * * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager. */ public function register_widgets( $widgets_manager ) { require_once( __DIR__ . '/my-elementor-widget.php' ); $widgets_manager->register( new \widget_class_name() ); } }
Now I don’t have fatal error anymore, but in my elementor, I still don’t find my widget.
Did I miss something or done something wrong ?
- The topic ‘Register new widgets in functions.php’ is closed to new replies.