自定义小工具未保存到侧边栏

时间:2019-05-28 作者:Andreas

我试图在wordpress 5.2.1中实现一个自定义小部件,但该小部件实际上并没有添加到侧栏中,并且在重新加载后再次丢失。

这是我的小部件代码:


class DW_Center_Address_Widget extends WP_Widget {

    const ID = \'dw-center-address\';

    var $formDefinition;

    function __construct() {

        parent::__construct(
            self::ID,
            // name of the widget
            Connector::get_translation(\'DW Center Address\'),
            // widget options
            array (
                \'description\' => Connector::get_translation( \'Displays the address of a Center stored in the SSO.\' )
            )
        );

        $this->formDefinition = [
            [
                \'type\' => \'info\',
                \'label\' => Connector::get_translation("Change Center data at the:") . \'<br/>\' . \'<a target="_blank" class="button" style="margin-top: 10px;" href="the.sso">\' . Connector::get_translation(\'SSO\') . \'</a>\'
            ],
            [
                \'type\' => \'text\',
                \'key\' => Shortcodes::ATTR_SLUG,
                \'id\' => $this->get_field_id(Shortcodes::ATTR_SLUG),
                \'name\' => $this->get_field_name(Shortcodes::ATTR_SLUG),
                \'label\' => Connector::get_translation(\'Center Slug or Id\'),
                \'value\' => \'\' // the default value
            ]
        ];

    }

    function form( $instance ) {
        for ($i = 0; $i < count($this->formDefinition); $i++) {
            if (array_key_exists(\'key\', $this->formDefinition[$i])) {
                $key = $this->formDefinition[$i][\'key\'];

                if (array_key_exists($key, $instance)) {
                    $this->formDefinition[$i][\'value\'] = $instance[$this->formDefinition[$i][\'key\']];
                }
            }
        }

        // generates the form html
        echo Template::render(\'admin/forms/widget_form.html.twig\', [
            \'form\' => $this->formDefinition
        ]);
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        foreach ($this->formDefinition as $def) {
            if (array_key_exists(\'key\', $def)) {
                $instance[ $def[\'key\'] ] = trim($new_instance[ $def[\'key\']]);
            }
        }

        return $instance;
    }

    function widget( $args, $instance ) {
        echo Shortcodes::dw_center($args);
    }

}

从我在其他地方看到的一切来看,它应该是有效的,但它并没有。。。

1 个回复
最合适的回答,由SO网友:Andreas 整理而成

好吧,我在几个小时的不眠之夜后找到了它:

呼叫$this->get_field_id($key) 在Widget构造函数中是一个错误。必须在function form() (可能是由于使用了一些全局变量*)

  • 大多数痛苦来自于这样一个事实,即它没有将小部件保存在侧栏中。原因是,在数据库的开发过程中,一切都变得一团糟。Wordpress似乎期望id_base 以及数据库中实际创建的实例。如果您想要清晰的图片,请尝试更改id_base 新鲜的东西
  • 最后的工作代码:

    class DW_Center_Address_Widget extends WP_Widget {
    
        // I changed the id_base a couple of times
        const ID = \'dwbn-center-address-3\';
    
        var $formDefinition;
        var $widgetArguments = [];
    
        function __construct() {
            //Constructor
            $widget_ops = array(\'classname\' => self::ID, \'description\' => Connector::get_translation( \'Displays the address of a Center stored in the SSO.\' ) );
            parent::__construct( self::ID, Connector::get_translation(\'DW Center Address\'), $widget_ops );
    
            $this->formDefinition = [
                // a title field makes the wordpress widget definetly more usefull, it will display this value in gray
                [
                    \'type\' => \'text\',
                    \'key\' => Shortcodes::ATTR_TITLE,
                    \'label\' => __(\'Title\'), // just take the standard translation
                    \'value\' => \'\' // the default value
                ],
                [
                    \'type\' => \'info\',
                    \'label\' => Connector::get_translation("Change Center data at the:") . \'<br/>\' . \'<a target="_blank" class="button" style="margin-top: 10px;" href="the.sso">\' . Connector::get_translation(\'SSO\') . \'</a>\'
                ],
                [
                    \'type\' => \'text\',
                    \'key\' => Shortcodes::ATTR_SLUG,
                    \'label\' => Connector::get_translation(\'Center Slug or Id\'),
                    \'value\' => \'\' // the default value
                ]
            ];
    
            foreach ($this->formDefinition as $def) {
                if (array_key_exists(\'key\', $def)) {
                    $this->widgetArguments[$def[\'key\']] = array_key_exists(\'value\', $def) ? $def[\'value\'] : \'\';
                }
            }
        }
    
        /**
         * @param array $args - holds context information
         * @param array $instance - the actual parameters
         */
        function widget($args, $instance) {
            echo Shortcodes::dw_center($instance);
        }
    
        function update($new_instance, $old_instance) {
            $instance = [];
    
            foreach ($this->widgetArguments as $key => $val) {
                $instance[ $key ] = trim(strip_tags($new_instance[ $key ]));
            }
    
            return $instance;
        }
    
        function form( $instance ) {
            $instance = wp_parse_args( (array) $instance, $this->widgetArguments );
            $form = [];
    
            for ($i = 0; $i < count($this->formDefinition); $i++) {
    
                $form[] = $this->formDefinition[$i];
    
                if (array_key_exists(\'key\', $form[$i])) {
                    $key = $form[$i][\'key\'];
    
                    if (array_key_exists($key, $instance)) {
                        $form[$i][\'value\'] = $instance[$key];
                    }
    
                    // it is important, that this functions are called during runtime, otherwise the widget ids will not be correct
                    $form[$i][\'id\'] = $this->get_field_id($key);
                    $form[$i][\'name\'] = $this->get_field_name($key);
                }
            }
    
            echo Template::render(\'admin/forms/widget_form.html.twig\', [
                \'form\' => $form
            ]);
    
        }
    }
    
    * 这是为什么不依赖全局名称空间的又一个例子——wordpress不会改变,但如果你构建了一个数百万人将使用的新系统,请尽量避免;)。

    相关推荐

    My widgets do not save

    每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'