0

I'm trying to create an "About Me Widget", as follows:

<?php
/**
* Plugin Name: About Me Widget
*/

class Custom_About_Me_Widget extends Wp_Widget {

    /**
    * Setup the Widget
    */
    public function __construct() {

        $widget_ops = array('classname'     => 'widget-about-author',
                            'description'   => __('A widget to display information about an author.') 
                            );

        $control_ops = array('name'         => 'Enter your name...',
                             'description'  => 'Some words about you...'
                            );

        parent::__construct( '_s_about_me_widget', __('_s: About Me Widget', '_s_about_me_widget'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );
        $title              = apply_filters( 'widget_title', $instance['title'] );
        $image              = $instance['image'];
        $description        = $instance['description'];

        echo $before_widget;

        if( $title ) {
            echo $before_title . $title . $after_title;
        } else {
            echo 'Widget has no title!';
        } ?>

        <div class="about-widge">
            <?php if( $image ): ?>
            <img src="<?php echo esc_url( $image );?>" alt="<?php echo esc_html( $title ); ?>"/>
            <?php endif; ?>

            <?php if( $description ): ?>
            <p><?php echo wp_kses_post( $description ); ?></p>
            <?php endif; ?>
        </div>

        <?php

        echo $after_widget;
    }

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

        $instance['title']          = strip_tags( $new_instance['title'] );
        $instance['image']          = strip_tags( $new_instance['image'] );
        $instance['description']    = $new_instance['description'];

        return $instance;
    }

    function form( $instance ) {
        $defaults = array(  'title'         => 'Your name here...',
                            'image'         => '',
                            'description'   => 'Enter some words about you...'
                        );

        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        ?>
        <!-- Form for Title -->
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title'];?>" />
        </p>

        <!-- Form for Image -->
        <p>
            <label for="<?php echo $this->get_field_id( 'image' ); ?>">Title:</label>
            <input id="<?php echo $this->get_field_id( 'image' ); ?>" name="<?php echo $this->get_field_name( 'image' ); ?>" value="<?php echo $instance['image'];?>" />
        </p>

        <!-- Form for Description -->
        <p>
            <label for="<?php echo $this->get_field_id( 'description' ); ?>">Title:</label>
            <textarea id="<?php echo $this->get_field_id( 'description' ); ?>" name="<?php echo $this->get_field_name( 'description' ); ?>" value="<?php echo $instance['description'];?>" ?>
            </textarea>
        </p>

    <?php

    }
}
add_action( 'widget_init', 'Custom_About_Me_Widget');
register_widget( 'Custom_About_Me_Widget' );

?>

Unfortunately, this returns:

Notice
: Undefined index: title in
C:\xampp\htdocs\wordpress\wp-content\themes\_s-master\inc\Widgets\widget_about-author-widget.php
on line
26

For each of the 3 indices: title, image, description. Why does this happen?

0

Shouldn't "function widget( $args, $instance )" be "public function widget( $args, $instance )"? Reference: WPBeginner

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.