我正在开发一个使用options API的插件。
我定义了一个使用函数清理输入的类,但当我提交选项表单时,它会显示两次错误和信息。
而且我没有成功地显示错误消息。
在验证选项表单时,显示成功和错误消息的正确方式是什么?
非常感谢。
register_setting(
$this->plugin_slug, // option_group
$this->plugin_slug, // option_name, for name property of tags
[$this, \'process_inputs\'] // sanitize_callback
);
public function process_inputs( $input ){
// sanitize functions:
// sanitize_email(), sanitize_file_name(), sanitize_html_class(), sanitize_key(), sanitize_meta(), sanitize_mime_type(),
// sanitize_option(), sanitize_sql_orderby(), sanitize_text_field(), sanitize_textarea_field(), sanitize_title(),
// sanitize_title_for_query(), sanitize_title_with_dashes(), sanitize_user()
$options = [];
if( isset( $input[\'frontend-font\'] ) and $input[\'frontend-font\'] == true ) {
$options[\'frontend-font\'] = true;
}else{
$options[\'frontend-font\'] = false;
}
if( isset( $input[\'backend-font\'] ) and $input[\'backend-font\'] == true ){
$options[\'backend-font\'] = true;
}else{
$options[\'backend-font\'] = false;
}
// add error/update messages
// check if the user have submitted the settings
// wordpress will add the "settings-updated" $_GET parameter to the url
if ( isset( $_GET[\'settings-updated\'] ) ) {
// add settings saved message with the class of "updated"*/
add_settings_error(
\'persianfont_messages\', // Slug title of setting
\'wporg_message\', // Slug-name , Used as part of \'id\' attribute in HTML output.
__( \'settings saved.\', \'persianfont\' ), // message text, will be shown inside styled <div> and <p> tags
\'error\' // Message type, controls HTML class. Accepts \'error\' or \'updated\'.
);
}
return $options;
}
/**
* Settings page display callback.
*/
function settings_page_content() {
// check user capabilities
if ( ! current_user_can( \'manage_options\' ) ) { return; }
//var_dump( wp_load_alloptions() ); // print all options
// show error/update messages
settings_errors( \'persianfont_messages\' );
?>
<div class="wrap">
<h1 class="wp-heading-inline"><?php echo esc_html($this->page_title); ?></h1>
<form method="post" action="options.php">
<?php
submit_button();
settings_fields( $this->plugin_slug ); // This prints out all hidden setting fields
do_settings_sections( $this->plugin_slug );
submit_button();
?>
</form>
</div>
<?php
}