如何在WP_Widget上显示错误

时间:2013-06-25 02:34:15

标签: wordpress

如何在表单,flash消息或某些方面显示错误,表明他做错了什么?

代码是这样的:

class My_Widget extends WP_Widget {
    public function update( $new_instance, $old_instance ) {
        if (!valid($new_instance))
            // How do I notify the user with a custom message
            return false;
        else
            return $new_instance
    }
}

我知道返回false会阻止保存选项,但用户不知道原因。

1 个答案:

答案 0 :(得分:1)

试试这个,

public function update( $new_instance, $old_instance ) {
    $old_instance['errors'] = array();
    if (!valid($new_instance)) {
          $$old_instance['errors']['myfield'] = 'Custom error mesasage goes here';
        // How do I notify the user with a custom message
        return false;
        //return $old_instance;
    }
    else
        return $new_instance
}

public function form($instance) {
    $myfieldMsg = (isset($instance['errors']) && isset($instance['errors']['myfield'])) ? $instance['errors']['myfield']) : null;
    echo $myfieldMsg;
    ....