首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >致命错误:找不到类'Elementor\Widget_Base‘

致命错误:找不到类'Elementor\Widget_Base‘
EN

Stack Overflow用户
提问于 2019-03-13 00:13:14
回答 1查看 3.7K关注 0票数 2

我正在尝试从我的插件中添加一个新的widget到elementor。我已经按照文档中的说明创建了elementor小部件:https://developers.elementor.com/creating-a-new-widget/

但问题是它不工作..No#1。当我使用自动加载时,它没有显示任何错误No#2。但当我使用require_once时,它显示了一个致命错误:Fatal error: Class 'Elementor\Widget_Base' not found

我的小部件代码

代码语言:javascript
复制
<?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

index.php中,我调用require_once( 'extensions/elementor/widget.php' );

它抛出这个错误:Fatal error: Class 'Elementor\Widget_Base' not found

但是当我在索引中使用自动加载函数时,它没有给出任何错误,也没有显示窗口小部件

代码语言:javascript
复制
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';
}

problem>可能是什么?

EN

回答 1

Stack Overflow用户

发布于 2020-04-08 21:58:18

开始时,Elementor类将不会被加载。因此,请使用初始化WordPress挂钩,因为该函数需要该文件并按照以下代码中的建议创建一个对象。

代码语言:javascript
复制
function load_elementor_widget() {
    require('your-php-code-that-extends-elementor-widget-class');

    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor\My_Widget_1() );
}
add_action('init', load_elementor_widget())

查看这个有助于理解的教程链接:https://develowp.com/build-a-custom-elementor-widget/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55126095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档