WP插件中意外的添加操作功能

时间:2021-11-17 作者:RRhodes

我正试图从一个教程中构建一个插件来访问我的谷歌日历。到目前为止,我只想注册这个小部件。以下是本教程的链接:

https://code.tutsplus.com/tutorials/how-to-create-a-google-calendar-plugin-for-wordpress--cms-32710

我一直遇到脚本底部的“add\\u action”函数的问题。我不断收到“语法错误,意外的”add\\u action“错误。我甚至不确定我是否正确构建了这个。

<?php

/** 
 * Plugin Name: My gCal Plugin
 * Description: Displays upcoming events from Google calender
 * version: 1.0.0
 * 
 * 
 */


class My_gCal_Widget extends WP_Widget
{
    public function __construct()
    {
        parent::__construct(
            \'my_gcal_widget\',
            \'My_gCal_Widget\',
            array(
                \'classname\' => \'my-gcal-widget\',
                \'description\' => \'Shows events from a calendar\'
            )
        );
    }

    public function widget($args, $instance)
    {
    }
}

add_action(\'widgets_init\', function () {
    register_widget(\'My_gCal_Widget\');
});

1 个回复
SO网友:Pat J

教程中并不完全清楚,但是add_action() 调用应该在类之外。

class My_gCal_Widget extends WP_Widget {
    public function __construct() {
        parent:: __construct(
            \'my_gcal_widget\',
            \'My_gCal_Widget\',
            array(
                \'classname\' => \'my-gcal-widget\',
                \'description\' => \'Shows events from a calendar\'
            )
        );
    }

    public function widget($args, $instance) { }

}

add_action(\'widgets_init\', function() {
    register_widget(\'My_gCal_Widget\');
});