The add_action i use is placed in function__construct():
add_action('admin_notices', array( $this, 'xyz_check_notices' ) );
the according method:
public function xyz_check_notices() {
if ( $this->check_fail_constraint ) {
add_settings_error( 'xyz-notices' , 'xyz-check-fail', __( 'Failed', 'xyzrequest'), 'error' );
}
else {
add_settings_error( 'xyz-notices', 'xyz-success', __('Worky Work', 'xyzrequest'), 'updated' );
}
settings_errors( 'xyz-notices' );
}
But somehow those messages are shown on every admin page and not only post save (and not alone but alongside the standard wp update message). i also tried to place the add_action into the method called by the save_post action hook. Nothing helped. So is there a way to properly hook into the save function that only the custom message according to the check_fail_constraint is shown? Thanks r.
]]>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;
}
}
I can not understand why the error does not exceed, any suggestions?
]]>register_setting('sbs_gig_options','sbs_gig_options','sbs_gig_validate_options');
add_settings_section('sbs_gig_main',__('Settings','sbs_gig'),'sbs_gig_lastfm_text','sbs_gig');
add_settings_field('sbs_gig_lastfm_key',__('Last FM Key','sbs_gig'),'sbs_gig_lastfm_key','sbs_gig','sbs_gig_main');
and the validation function looks like this:
function sbs_gig_validate_options( $input ) {
// Create our array for storing the validated options
$valid = array();
// Loop through each of the incoming options
foreach( $input as $key => $value ) {
// Check to see if the current option has a value. If so, process it.
if( isset( $input[$key] ) ) {
// Strip all HTML and PHP tags and properly handle quoted strings
$valid[$key] = strip_tags( stripslashes( $input[ $key ] ) );
if ($key=='lastfmkey') {
if (!sbs_gig_verify_lastfm($valid[$key])) {
add_settings_error('sbs_gig_lastfm_key','lastfmkey_error',__('Please enter a valid Last FM API Key','error'));
$valid['lastfmkey']='';
}
else {
$valid['lastfmkey']=$input[$key];
}
}
} // end if
} // end foreach
// Return the array processing any additional functions filtered by this action
return $valid;
}
From what I’ve read, the error I add via add_settings_error() should be displayed automatically, but it’s not. I have to explicitly call settings_error() to get the error to display. Can anyone suggest why?
]]>I’m building a theme with custom-options using Otto’s fantastic tutorial on the WordPress settings API as a basis for my custom theme options.
I have registered a new setting: iblaunchy_options
and all of my theme’s settings are stored inside of this option:
register_setting( 'iblaunchy_options', 'iblaunchy_options', 'iblaunchy_options_validate' );
I’m currently working on the validation function, and would like to be able to display errors on the options page if one of the options in my validation function fails to pass.
I’m not sure how to do this. I read up on the codex for add_settings_error and settings_errors but am still unsure how to proceed.
Has anyone come across a good explanation of how to use these settings api functions to display errors back to an options page built using the method in Otto’s tutorial?
I’m just not sure how to show errors for these settings because they are nested within the iblaunchy_options
setting.
Any ideas?
Thanks,
Greg J
]]>For instance, I have the following code:
register_setting("my_setting_group", "my_option_name", "validate_callback_function");
function validate_callback_function($input) {
if ($input is invalid) {
add_settings_error(???);
} else return $input;
}
What do I use inside the add_settings_error() function, and how do I make the admin page display the error(s) if it is not automatic?
]]>