I am trying a to add a new widget from my plugin to elementor. I have followed the documentation to how to create an elementor widget: https://developers.elementor.com/creating-a-new-widget/
But problem is its not working..
No#1. When i am using autoload its not showing any error
No#2. But when i am using require_once its shows a fatal error: Fatal error: Class 'Elementor\Widget_Base' not found
<?php
namespace WPEVENTCAL\extensions\elementor;
class widget extends \Elementor\Widget_Base {
    public function get_name() {
        return 'Aembed';
    }
    public function get_title() {
        return __( 'oEmbed', 'plugin-name' );
    }
    public function get_icon() {
        return 'fa fa-code';
    }
    public function get_categories() {
        return [ 'basic' ];
    }
    protected function _register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Content', 'plugin-name' ),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'url',
            [
                'label' => __( 'URL to embed', 'plugin-name' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'input_type' => 'url',
                'placeholder' => __( 'https://your-link.com', 'plugin-name' ),
            ]
        );
        $this->end_controls_section();
    }
    protected function render() {
        $settings = $this->get_settings_for_display();
        $html = wp_oembed_get( $settings['url'] );
        echo '<div class="oembed-elementor-widget">';
        echo ( $html ) ? $html : $settings['url'];
        echo '</div>';
    }   
}
-Main plugin file
    -extension
        -elementor
            -widget.php
    - index.php
In index.php i am calling require_once( 'extensions/elementor/widget.php' );
It throws that error : Fatal error: Class 'Elementor\Widget_Base' not found
But when i use autoload function in index it doesn't gives any error neither shows widget
use WPEVENTCAL\extensions\elementor\index;
function autoload($class = '') {
    if (!strstr($class, 'WPEVENTCAL')) {
        return;
    }
    $result = str_replace('WPEVENTCAL\\', '', $class);
    $result = str_replace('\\', '/', $result);
    require $result . '.php';
}
what may be the problem>?
At the start, the Elementor class will not be loaded. So use the init WordPress hooks, in that function require the file and create an object as suggested in the below code.
function load_elementor_widget() {
    require('your-php-code-that-extends-elementor-widget-class');
    
    \Elementor\Plugin::instance()->widgets_manager->register( new \Elementor\My_Widget_1() );
}
add_action('init', 'load_elementor_widget')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With