调用我的函数时,add_action给出错误

时间:2020-05-16 13:13:18

标签: php wordpress

我有下面的代码,但是wp说我:

警告:call_user_func_array()期望参数1为有效的回调,/home/deniztas/hekim.deniz-tasarim.site/wp-includes/class-wp-hook中找不到函数“ widget_categories”或无效的函数名称.php行287

那么,为什么我不能添加功能?我必须做什么?

<?php
/**
 * Plugin Name: Hekim - Elementor Extension
 * Description: For Hekim Theme
 * Plugin URI:  https://themeforest.net/user/helvatica
 * Version:     1.0.0
 * Author:      Helvatica Themes
 * Author URI:  https://themeforest.net/user/helvatica
 * Text Domain: hekim-plugin-elementor-extension
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
/**
 * Main Elementor Test Extension Class
 *
 * The main class that initiates and runs the plugin.
 *
 * @since 1.0.0
 */
   final class Hekim_Elementor {


        /**
         * widget_categories
         *
         * Register new category for widgets.
         *
         * @since 1.2.0
         * @access public
         */
        public function widget_categories( $elements_manager ) {

            $elements_manager->add_category(
                'hero-section',
                [
                    'title' => esc_html__( 'Hero Sections for elemntor', 'megaaddons' ),
                    'icon' => 'fa fa-plug',
                ]
            );

        }

    etc.etc.
    }
    add_action( 'elementor/elements/categories_registered', 'widget_categories' );
    ?>

1 个答案:

答案 0 :(得分:0)

我认为您想在Elementor中添加小部件类别,以将小部件组织为组(请更清楚地了解下次要在问题中达到的目标)。因此,如果这是您想要实现的,那么您的插件代码应如下所示:

<?php
/**
 * Plugin Name: Hekim - Elementor Extension
 * Description: For Hekim Theme
 * Plugin URI:  https://themeforest.net/user/helvatica
 * Version:     1.0.0
 * Author:      Helvatica Themes
 * Author URI:  https://themeforest.net/user/helvatica
 * Text Domain: hekim-plugin-elementor-extension
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

function add_elementor_widget_categories( $elements_manager ) {

    $elements_manager->add_category(
        'hero-section',
        [
            'title' => esc_html__( 'Hero Sections for elemntor', 'megaaddons' ),
            'icon' => 'fa fa-plug',
        ]
    );

}
add_action( 'elementor/elements/categories_registered', 'add_elementor_widget_categories' );


?>

我在文档中找到了正确的结构:https://developers.elementor.com/widget-categories/

您不需要该类和所有其他内容,只需使用add_action进行连接即可。

希望这会有所帮助。