WP 插件我收到通知:未定义的索引:创建小部件时出错 "instance" 未定义

WP Plugin I get Notice: Undefined index: error when creating widget "instance" not defined

一直在更新一个旧的但我最喜欢的不受支持的插件之一。我已经使所有功能成为当前功能并且插件运行良好只有几个错误需要修复 运行 in define('WP_DEBUG', true); 所以这个插件尝试加载默认值但是我在创建新的小部件天气时遇到错误或没有默认广告。我一直在寻找一种更好的方法来做到这一点,但没有运气。问题是 $instance['name'] 在保存小部件之前保持未定义状态。将不胜感激谢谢

我得到的错误 这个

的一个实例

注意:未定义索引:第 56 行 /var/www/public_html/wp-content/plugins/advertising-manager/lib/Advman/Widget.php 中的名称

我为每个结果得到这个

注意:未定义索引:第 58 行 /var/www/public_html/wp-content/plugins/advertising-manager/lib/Advman/Widget.php 中的名称

这是代码小部件 php

    <?php
class Advman_Widget extends WP_Widget
{
    function Advman_Widget()
    {
        $widget_ops = array('classname' => 'Advman_Widget', 'description' => __('Place an ad', 'advman'));
        parent::__construct('advman', __('Advertisement', 'advman'), $widget_ops);
    }
    function widget($args, $instance)
    {
        extract($args); //nb. $name comes out of this, hence the use of $n
        global $advman_engine;

        $n = $instance['name'] == '' ? $advman_engine->getSetting('default-ad') : $instance['name'];
        $ad = $advman_engine->selectAd($n);

        if (!empty($ad)) {
            $suppress = !empty($instance['suppress']);
            $title = apply_filters('widget_title', $instance['title']);

            $ad_code = $ad->display();
            echo $suppress ? ($title . $ad_code) : ($before_widget . $before_title . $title . $after_title . $ad_code . $after_widget);
            $advman_engine->incrementStats($ad);
        }
    }

    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $instance['title'] = strip_tags(stripslashes($new_instance['title']));
        $instance['suppress'] = !empty($new_instance['suppress']);
        $instance['name'] = $new_instance['name'];
        return $instance;
    }

    function form($instance)
    {
        global $advman_engine;

        $ads = $advman_engine->getAds();
        $names = array();
        // Loop through ads, and get only unique ad names
        foreach ($ads as $ad) {
            $names[$ad->name] = $ad->name;
        }

    $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
    $checked = !empty($instance['suppress']) ? 'checked="checked"' : '';
?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'advman'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
            <p>
                <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Select an ad to display:', 'advman'); ?>
                <select class="widefat" id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>">
                    <option value=""<?php echo $instance['name'] == "" ? " selected='selected'" : ''; ?>><?php _e('Default Ad', 'advman'); ?></option>
<?php foreach ($names as $name) : ''?>
                    <option value="<?php echo $name; ?>"<?php echo $name == $instance['name'] ? " selected='selected'" : ''; ?>>#<?php echo $name; ?></option>
<?php endforeach; ?>
                </select>
                </label>
            </p>
            <p><label for="<?php echo $this->get_field_id('suppress'); ?>"><?php _e('Hide widget formatting', 'advman'); ?> <input class="checkbox" id="<?php echo $this->get_field_id('suppress'); ?>" name="<?php echo $this->get_field_name('suppress'); ?>" type="checkbox" <?php echo $checked; ?> /></label></p>
        <?php
    }
}
?>

我发现我的问题有时做一些其他事情会有所帮助

在可以做出任何选择之前,我在作业中使用了 isset()

我的修复

// I Added 
if(!isset($instance['name']) || empty($instance['name'])) { 
$instance['name'] = ""; 




//Added it here
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
if(!isset($instance['name']) || empty($instance['name'])) { 
$instance['name'] = ""; 
}   
$checked = !empty($instance['suppress']) ? 'checked="checked"' : '';