• Resolved Luke

    (@lukejanicke)


    I’m trying to get the official (i.e. Settings API) “Settings updated” notification to work in my theme options. I have a working theme options page and the options are saving. I just want the notifications to work.

    Could someone please help me out with the code?

    The codex and the few blog posts about using add_settings_error are not clear enough for me.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Luke

    (@lukejanicke)

    Seems to be working. Not sure how.

    This is all in my theme’s functions.php file.

    function respite_register_settings() {
    	$copyright = sprintf( '© %s %s. All rights reserved.', date("Y"), get_bloginfo( 'name', 'display' ) );
    	add_option( 'copyright', $copyright );
    	register_setting( 'respite', 'copyright', 'respite_sanitize_options' );
    }
    add_action( 'admin_init', 'respite_register_settings' );
    
    function respite_sanitize_options( $input ) {
    	add_settings_error(
    		'respite',
    		esc_attr('settings_updated'),
    		__('Settings saved.'),
    		'updated'
    	);
    	return $input;
    }
    
    function respite_admin_notices() {
         settings_errors( 'respite' );
    }
    add_action( 'admin_notices', 'respite_admin_notices' );
    
    function respite_register_options_page() {
    	add_theme_page('Theme Options', 'Theme Options', 'manage_options', 'respite-options', 'respite_options_page');
    }
    add_action('admin_menu', 'respite_register_options_page');
    
    function respite_options_page() { ?>
    <div class="wrap">
    	<h2>Theme Options</h2>
    	<form method="post" action="options.php">
    		<?php settings_fields( 'respite' ); ?>
    		<table class="form-table">
    			<tr valign="top">
    				<th scope="row"><label for="copyright">Copyright notice</label></th>
    				<td><input type="text" id="copyright" name="copyright" value="<?php echo get_option('copyright'); ?>" /></td>
    			</tr>
    		</table>
    		<?php submit_button(); ?>
    	</form>
    </div>
    <?php }
    Thread Starter Luke

    (@lukejanicke)

    I just added a second option. Now I get two update notifications each time I save the settings.

    Help…

    Thread Starter Luke

    (@lukejanicke)

    OK. I did it. But I still don’t fully understand the Settings API fully.

    Here is my entire options.php file.

    <?php
    
    function respite_options_setup() {
    	$notfound = '<h1 class="aligncenter">Not found</h1><p>The link you followed here doesn’??t match any content on this site.</p>';
    	$copyright = sprintf( '&copy; %s %s. All rights reserved.', date("Y"), get_bloginfo( 'name', 'display' ) );
    	add_option( 'notfound', $notfound );
    	add_option( 'copyright', $copyright );
    }
    add_action( 'after_setup_theme', 'respite_options_setup' );
    
    function respite_notfound() {
    	$notfound = get_option( 'notfound' );
    	if ( $notfound ) printf( '%s', $notfound );
    }
    
    function respite_copyright() {
    	$copyright = get_option( 'copyright' );
    	if ( $copyright ) printf( '<div id="copyright">%s</div>', $copyright );
    }
    
    function respite_register_settings() {
    	register_setting( 'respite-settings-group', 'notfound' );
    	register_setting( 'respite-settings-group', 'copyright' );
    	register_setting( 'respite-settings-group', 'update_notification', 'respite_settings_update_notification' );
    }
    add_action( 'admin_init', 'respite_register_settings' );
    
    function respite_settings_update_notification( $input ) {
    	add_settings_error(
    		'respite-settings-updated',
    		esc_attr('settings_updated'),
    		__('Settings saved.'),
    		'updated'
    	);
    	return $input;
    }
    
    function respite_admin_notices() {
         settings_errors( 'respite-settings-updated' );
    }
    add_action( 'admin_notices', 'respite_admin_notices' );
    
    function respite_register_options_page() {
    	add_theme_page('Theme Options', 'Theme Options', 'manage_options', 'respite-options', 'respite_options_page');
    }
    add_action('admin_menu', 'respite_register_options_page');
    
    function respite_options_page() { ?>
    <div class="wrap">
    	<h2>Theme Options</h2>
    	<p><em>Note: Respite has two custom menu locations: main navigation and footer links.</em></p>
    	<form method="post" action="options.php">
    		<?php settings_fields( 'respite-settings-group' ); ?>
    		<table class="form-table">
    			<tr valign="top">
    				<th scope="row"><label for="notfound">Not found notice</label></th>
    				<td><input style="width: 100%;" type="text" id="notfound" name="notfound" value='<?php echo get_option('notfound'); ?>' /><br />You may use HTML and CSS</td>
    			</tr>
    			<tr valign="top">
    				<th scope="row"><label for="copyright">Copyright notice</label></th>
    				<td><input style="width: 100%;" type="text" id="copyright" name="copyright" value='<?php echo get_option('copyright'); ?>' /><br />You may use HTML and CSS</td>
    			</tr>
    		</table>
    		<?php submit_button(); ?>
    	</form>
    </div>
    <?php }
    
    ?>
    Thread Starter Luke

    (@lukejanicke)

    As you can see, I simply created a new setting, and made a special sanitize callback function for that setting only. In that callback function, I used add_settings_error. Strangely, I didn’t need to add a form element on the options page for this setting. I guess you could choose any one of the actual options to trigger add_settings_error.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Settings updated notification’ is closed to new replies.