0

tell me how to update the widget and check the fields, to implement the output error in the case of incorrectly filled data.

It may have built the functional?

class My_Widget extends WP_Widget {

/**
 * Sets up the widgets name etc
 */
public function __construct() {
    $widget_ops = array( 
        'classname' => 'my_widget',
        'description' => 'My Widget is awesome',
    );
    parent::__construct( 'my_widget', 'My Widget', $widget_ops );
}

/**
 * Outputs the content of the widget
 *
 * @param array $args
 * @param array $instance
 */
public function widget( $args, $instance ) {
    echo $args['before_widget'];        
    echo $instance['title'];
    echo $args['after_widget'];
}

/**
 * Outputs the options form on admin
 *
 * @param array $instance The widget options
 */
public function form( $instance ) {
    $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
    ?>
    <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label> 
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
    </p>
    <?php 
}

/**
 * Processing widget options on save
 *
 * @param array $new_instance The new options
 * @param array $old_instance The previous options
 */
    public function update( $new_instance, $old_instance ) {


    // !!!!!!!
    if(empty($new_instance['title'])){
    // !!!!!
    // How to make it clear to the user that he incorrectly filled field?
    // !!!!!
return 'Error!';    
    }}}

question function "update($new_instance, $old_instance)"

2
  • Can you elaborate more? we need a bit of context, usually you can do that in the definition of your widget
    – David Lee
    Commented Feb 12, 2017 at 20:58
  • I added a simple example
    – TrubinE
    Commented Feb 12, 2017 at 21:09

1 Answer 1

0

Try this:

class My_Widget extends WP_Widget {

    /**
     * Sets up the widgets name etc
     */
    public function __construct() {
        $widget_ops = array(
            'classname' => 'my_widget',
            'description' => 'My Widget is awesome',
        );
        parent::__construct('my_widget', 'My Widget', $widget_ops);
    }

    /**
     * Outputs the content of the widget
     *
     * @param array $args
     * @param array $instance
     */
    public function widget($args, $instance) {
        echo $args['before_widget'];
        echo $instance['title'];
        echo $args['after_widget'];
    }

    /**
     * Outputs the options form on admin
     *
     * @param array $instance The widget options
     */
    public function form($instance) {

        $title = !empty($instance['title']) ? $instance['title'] : esc_html__('New title', 'text_domain');
        if(isset($instance['error'])){
            echo $instance['error'];
        }
        ?>
            <p>
            <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_attr_e('Title:', 'text_domain'); ?></label> 
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
            </p>
        <?php
    }

    /**
     * Processing widget options on save
     *
     * @param array $new_instance The new options
     * @param array $old_instance The previous options
     */
    public function update($new_instance, $old_instance) {

        // !!!!!!!
        if (empty($new_instance['title'])) {
            $instance['error'] = "ERROR!! title empty";
        }else{
            $instance['title'] = strip_tags( $new_instance['title'] );
        }
        return $instance;
    }

}

you can also check for the error value in the widget function to say that the data is not valid and the widget data needs to be updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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