WordPress自定义小工具未定义变量通知

时间:2016-06-15 作者:Hans Desjarlais

我正在开发一个主题,并为其创建了一个自定义小部件。

当我打开调试模式时,我得到以下注意:,

NOTICE: wp-content/themes/integral/inc/widgets/feature_widget.php:92 - Undefined variable: title 
wp_list_widgets, call_user_func_array, wp_widget_control, call_user_func_array, WP_Widget->form_callback, Integral_Feature_Widget->form

NOTICE: wp-content/themes/integral/inc/widgets/feature_widget.php:97 - Undefined variable: text
wp_list_widgets, call_user_func_array, wp_widget_control, call_user_func_array, WP_Widget->form_callback, Integral_Feature_Widget->form

NOTICE: wp-content/themes/integral/inc/widgets/feature_widget.php:110 - Undefined variable: textarea
wp_list_widgets, call_user_func_array, wp_widget_control, call_user_func_array, WP_Widget->form_callback, Integral_Feature_Widget->form
这是小部件文件的内容,

<?php 
/**
 * new WordPress Widget format
 * Wordpress 2.8 and above
 * @see http://codex.wordpress.org/Widgets_API#Developing_Widgets
 */
class Integral_Feature_Widget extends WP_Widget {

    /**
     * Constructor
     *
     * @return void
     **/
    function __construct() {
        $widget_ops = array( \'classname\' => \'wcp_image\', \'description\' => \'Add a feature to the homepage features section.\' );
        parent::__construct( \'Integral_feature\', \'Integral - Feature Widget\', $widget_ops );
    }

    /**
     * Outputs the HTML for this widget.
     *
     * @param array  An array of standard parameters for widgets in this theme
     * @param array  An array of settings for this widget instance
     * @return void Echoes it\'s output
     **/
    function widget( $args, $instance ) {
        extract( $args, EXTR_SKIP );

       // these are the widget options
       $title = apply_filters(\'widget_title\', $instance[\'title\']);
       $text = $instance[\'text\'];
       $image_url = $instance[\'image_url\'];
       $textarea = apply_filters( \'widget_textarea\', empty( $instance[\'textarea\'] ) ? \'\' : $instance[\'textarea\'], $instance );
       echo $before_widget;
       // Display the widget
       echo \'\';

       // Check if text is set
       if( $text ) {
          echo \'<i class="fa \'.$text.\' pull-left featureicon"></i>\';
       }
       if( !$text && $image_url) {
          echo \'<img  src="\'.$image_url.\'" class="fimage">\';
       }

       // Check if title is set
       if ( $title ) {
          echo $before_title . $title . $after_title;
       }

       // Check if textarea is set
       if( $textarea ) { echo wpautop($textarea); }
       echo \'\';
       echo $after_widget;
    }

    /**
     * Deals with the settings when they are saved by the admin. Here is
     * where any validation should be dealt with.
     *
     * @param array  An array of new settings as submitted by the admin
     * @param array  An array of the previous settings
     * @return array The validated and (if necessary) amended settings
     **/
    function update( $new_instance, $old_instance ) {

        // update logic goes here
        $instance = $old_instance;
          // Fields
        $instance[\'title\'] = strip_tags($new_instance[\'title\']);
        $instance[\'text\'] = strip_tags($new_instance[\'text\']);
        $instance[\'image_url\'] = strip_tags($new_instance[\'image_url\']);
        if ( current_user_can(\'unfiltered_html\') )
            $instance[\'textarea\'] =  $new_instance[\'textarea\'];
        else $instance[\'textarea\'] = stripslashes( wp_filter_post_kses( addslashes($new_instance[\'textarea\']) ) );

        return $instance;
    }

    /**
     * Displays the form for this widget on the Widgets page of the WP Admin area.
     *
     * @param array  An array of the current settings for this widget
     * @return void Echoes it\'s output
     **/
    function form( $instance ) {
        extract($instance);

?>
    <p>
    <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Feature Title\', \'integral\'); ?></label>
    <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; ?>" />
    </p>

    <p>
    <label for="<?php echo $this->get_field_id(\'text\'); ?>"><?php _e(\'Feature Icon Class\', \'integral\'); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id(\'text\'); ?>" name="<?php echo $this->get_field_name(\'text\'); ?>" type="text" value="<?php echo $text; ?>" />
    <small>Copy and paste the required icon class from the <a href="https://fortawesome.github.io/Font-Awesome/cheatsheet/" target="_blank">Fontawesome Icons List</a> and choose from 600+ icons.</small>
    </p>
    <p><label for="<?php echo $this->get_field_id(\'image_url\'); ?>"><?php _e(\'Feature Image\', \'integral\'); ?></label>
    <br /><small>Or instead of using an icon you can upload an image.</small>
    <input id="<?php echo $this->get_field_id(\'image_url\'); ?>" type="text" class="image-url" name="<?php echo $this->get_field_name(\'image_url\'); ?>" value="<?php if (isset($image_url)) echo esc_attr($image_url); ?>" style="width: 100%;" />
    <input data-title="Image in Widget" data-btntext="Select it" class="button upload_image_button" type="button" value="<?php _e(\'Upload\',\'integral\') ?>" /> <input data-title="Image in Widget" data-btntext="Select it" class="button clear_image_button" type="button" value="<?php _e(\'Clear\',\'integral\') ?>" />
    </p>
    <p class="img-prev">
        <?php if (isset($image_url) && $image_url) { echo \'<img src="\'.$image_url.\'" style="max-width: 100%;">\';} ?>
    </p>
    <p>
    <label for="<?php echo $this->get_field_id(\'textarea\'); ?>"><?php _e(\'Feature Description\', \'integral\'); ?></label>
    <textarea class="widefat" rows="5" id="<?php echo $this->get_field_id(\'textarea\'); ?>" name="<?php echo $this->get_field_name(\'textarea\'); ?>"><?php echo $textarea; ?></textarea>
    <small>No limit on the amount of text and HTML is allowed.</small>
    </p>
<?php
    }
}
// End of Widget Class
add_action( \'widgets_init\', create_function( \'\', "register_widget( \'Integral_Feature_Widget\' );" ) );
?>
我想我必须为这些字段设置一个默认变量,但我不知道怎么做?有什么想法吗?

1 个回复
SO网友:cjbj

你可以你wp_parse_args 要合并$instance 使用默认数组。所以,你会开始form 功能不具有extract($instance) 但是有

$defaults = array( 
    \'title\'         => \'Your title\',
    \'text\'          => \'...\',
    \'image_url\'     => \'...\'
    \'textarea\'      => \'...\'
    );
$instance = wp_parse_args((array) $instance, $defaults);

相关推荐

Debug.log是否执行日志轮换?

这更像是一个调查因此,我将WP\\u DEBUG\\u日志启用为true但它确实可以工作,调试。日志位于我的wp内容目录中<但是它是否也会像每天不同的文件那样进行日志轮换示例:今天的文件-调试。记录昨天的2019-12-18-101010-debug。日志它是这样工作的吗?或者只是调试。全部注销?