How to register the new Elementor Widget in Functions

I’m trying to register a new Element widget without creating a plugin. so i want to register it in my functions.php

With regards to the Elementor Developer’s Guide, I needed to use:

function register_new_widgets( $widgets_manager ) {

    require_once( __DIR__ . '/widgets/widget-1.php' );
    require_once( __DIR__ . '/widgets/widget-2.php' );

    $widgets_manager->register( new \Elementor_Widget_1() );
    $widgets_manager->register( new \Elementor_Widget_2() );

}
add_action( 'elementor/widgets/register', 'register_new_widgets' );

Probably, I was getting a fatal error saying that my class does not exist. After a long search I found that Elementor class is not loaded yet while registering this new class and I need to use Elementor initialization to load my widget when Elementor will load other widgets.

So with respect to this document, I’ve created something similar:

final class Plugin {

    /**
     * Initialize
     *
     * Load the addons functionality only after Elementor is initialized.
     *
     * Fired by `elementor/init` 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 `elementor/widgets/register` action hook.
     *
     * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager.
     */
    public function register_widgets( $widgets_manager ) {

        require_once( __DIR__ . '/includes/widgets/widget-1.php' );
        require_once( __DIR__ . '/includes/widgets/widget-2.php' );

        $widgets_manager->register( new \Widget_1() );
        $widgets_manager->register( new \Widget_2() );

    }

}

So now I don’t have any more fatal errors, but I can’t see my widgets in my Elementor widget.

Did I do it right ? Or did I miss something ?

Leave a Comment