簡體   English   中英

我得到WP插件注意:未定義索引:創建未定義小部件“實例”時出錯

[英]WP Plugin I get Notice: Undefined index: error when creating widget “instance” not defined

正在更新舊但我最喜歡的不受支持的插件之一。 我已經將所有功能設為當前功能,並且該插件運行良好,僅在運行define('WP_DEBUG', true);時出現了幾個錯誤即可修復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"' : '';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM