【问题标题】:add_action gives error when call my function调用我的函数时 add_action 出错
【发布时间】:2020-05-16 13:13:18
【问题描述】:

我有以下代码,但 wp 说我:

警告:call_user_func_array() 期望参数 1 是有效的回调,未找到函数“widget_categories”或 /home/deniztas/hekim.deniz-tasarim.site/wp-includes/class-wp-hook 中的函数名称无效.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' );
    ?>

【问题讨论】:

  • 函数widget_categories,是在另一个函数里面吗?因为你在 etc. etc. 下面还有另一个 } ?找不到函数 widget_categories。我知道这是公开的。但是您可以尝试将其从其他功能中取出来检查它是否正常工作。你能描述一下 $elements_manager 参数中的数据是什么样的吗?
  • 我更新了问题,你能再看一遍吗?

标签: php wordpress


【解决方案1】:

我认为您想在 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 挂钩。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 2011-04-26
    • 2014-07-11
    • 2015-11-12
    • 2011-04-05
    • 1970-01-01
    • 2015-07-17
    • 2012-07-22
    相关资源
    最近更新 更多