构建自定义的Elementor Widget Wordpress



我想创建一个elementor小部件,并将其添加到基本的elementor菜单中。我根据本教程找到并执行此操作,但它不起作用(不出现在基本菜单中(:https://develowp.com/build-a-custom-elementor-widget/在此处输入图像描述。我使用调试,我认为原因可能是以下代码:"add_action('elementor/widgets/widgets_registered',[$this,'register_widgets'](;"有人能帮我解决这个问题吗?或者有人能正确运行其他代码吗?

**创建自定义Elementor Widget与创建本地WordPress小部件没有太大区别,基本上是从创建一个扩展Widget_Base类的类开始,并填充所有所需的方法。

每个小部件都需要有一些基本设置,比如在代码中识别小部件的唯一名称、用作小部件标签的标题和图标。除此之外,我们还有一些高级设置,如小部件控件,这些控件基本上是用户选择自定义数据的字段,以及基于小部件控件中的用户数据生成最终输出的渲染脚本。**

<?php
/**
* Elementor oEmbed Widget.
*
* Elementor widget that inserts an embbedable content into the page, from 
any given URL.
*
* @since 1.0.0
*/
class Elementor_oEmbed_Widget extends ElementorWidget_Base {
/**
* Get widget name.
*
* Retrieve oEmbed widget name.
*
* @since 1.0.0
* @access public
*
* @return string Widget name.
*/
public function get_name() {
return 'oembed';
}
/**
* Get widget title.
*
* Retrieve oEmbed widget title.
*
* @since 1.0.0
* @access public
*
* @return string Widget title.
*/
public function get_title() {
return __( 'oEmbed', 'plugin-name' );
}
/**
* Get widget icon.
*
* Retrieve oEmbed widget icon.
*
* @since 1.0.0
* @access public
*
* @return string Widget icon.
*/
public function get_icon() {
return 'fa fa-code';
}
/**
* Get widget categories.
*
* Retrieve the list of categories the oEmbed widget belongs to.
*
* @since 1.0.0
* @access public
*
* @return array Widget categories.
*/
public function get_categories() {
return [ 'general' ];
}
/**
* Register oEmbed widget controls.
*
* Adds different input fields to allow the user to change and customize the 
widget settings.
*
* @since 1.0.0
* @access protected
*/
protected function _register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => __( 'Content', 'plugin-name' ),
'tab' => ElementorControls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'url',
[
'label' => __( 'URL to embed', 'plugin-name' ),
'type' => ElementorControls_Manager::TEXT,
'input_type' => 'url',
'placeholder' => __( 'https://your-link.com', 'plugin-name' ),
]
);
$this->end_controls_section();
}
/**
* Render oEmbed widget output on the frontend.
*
* Written in PHP and used to generate the final HTML.
*
* @since 1.0.0
* @access protected
*/
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>';
}
}

最新更新