【问题标题】:Creating custom widget in wordpress在 wordpress 中创建自定义小部件
【发布时间】:2020-06-08 09:52:57
【问题描述】:

我正在尝试为 wordpress 创建一个简单的插件小部件,就像这样

<?php

// The widget class
class My_Custom_Widget extends WP_Widget {

    // Main constructor
    public function __construct() {
        parent::__construct(
            'my_custom_widget',
            __( 'My Custom Widget', 'text_domain' ),
            array(
                'customize_selective_refresh' => true,
            )
        );
    }

    // The widget form (for the backend )
    public function form( $instance) {}

    // Update widget settings
    public function update($new_instance, $old_instance) {}

    public function helloWorld(){
        echo 'Hello World';
    }

    // Display the widget
    public function widget( $args, $instance ) {
        helloWorld();
    }
}

// Register the widget
function my_register_custom_widget() {
    register_widget( 'My_Custom_Widget' );
}
add_action( 'widgets_init', 'my_register_custom_widget' );

您会看到在函数小部件内部我调用了函数 helloWorld(),但是在显示这样的小部件时出现错误

致命错误:在第 38 行的 /wp-content/plugins/my-widget-plugin/my-widget-plugin.php 中调用未定义函数 helloWorld()

为什么我不能在函数内部调用函数?

【问题讨论】:

  • 它会调用哪个helloWorld()

标签: php wordpress


【解决方案1】:

你忘了加$this:

    // Display the widget
    public function widget( $args, $instance ) {
        $this->helloWorld();
    }

希望能帮到你。

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多