如何在REGISTER_SETTING回调中使用ADD_SETTINGS_ERROR

时间:2013-01-02 作者:user1692333

public function database_name_check($input){
    //debug
    add_settings_error(\'notice\', \'lol\', \'lol\');
    if($this->wpdb->get_var($this->wpdb->prepare(\'SHOW DATABASES LIKE %s\', $input)) == $input)
        return $input;
    else{
        add_settings_error(\'notice\', \'lol\', \'lol\');
        return FALSE;
    }
}
我不明白为什么误差不超过,有什么建议吗?

1 个回复
最合适的回答,由SO网友:chrisguitarguy 整理而成

看看add_settings_error 原型

add_settings_error( $setting, $code, $message, $type );
第一个参数是您的设置名称/键,或者如果您的设置位于另一个页面上(例如general) 它应该是页面键。第二个是要添加到ID属性的内容,然后是错误/更新消息,最后是键入。它不起作用,因为您使用不正确。

所以你可能想。。。

<?php
add_settings_error(
    \'your_setting_key\', // whatever you registered in `register_setting
    \'a_code_here\', // doesn\'t really mater
    __(\'This is the message itself\', \'wpse\'),
    \'error\', // error or notice works to make things pretty
);
您还需要告诉WordPress显示您的设置错误。如果它位于自定义页面上,则需要包括settings_errors 在回调中。

settings_errors(\'your_setting_key\');

结束

相关推荐

Displaying oEmbed errors?

有时,通过oEmbed嵌入项目是不可能的,例如,当YouTube视频已禁用嵌入时。The oEmbed service will return a 401 Unauthorized, 并且不会转换代码。有没有办法通知用户这一点?当前的工作流是非直观的(至少对我来说),我更喜欢在WordPress页面上,或者更好的是,在编辑器中显示一条消息,说明对象无法嵌入。