0

I am developing a theme and I have created a custom widget for it.

When I turn on debug mode I get the following notices,

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

This is the contents of the widget file,

<?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' );" ) );
?>

I think I have to set a default variable for those fields, but I am not sure how to do it? Any ideas?

1

You can you wp_parse_args to merge $instance with a default array. So, you would start you form function not with extract($instance) but with

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.