192 PART III • Using Plugins with WordPress
Then you add the function smashings_settingsdemo_init() to the
admin_init hook.
Next, add a little something to the section just to prove that you can:
// Runs at the start of the section
function smashing_settings_section_callback() {
echo '<p>This is the section intro.</p>';
}
You’ll recognize the function name, smashing_settings_section_callback(), from
the earlier add_settings_section(). It is the callback function, so this will run at the
beginning of the section, echoing a p tag with some text.
In a similar fashion, create the callback function for the add_settings_field() you
created, called smashing_sample_input_callback():
// The input sample setting
function smashing_sample_input_callback() {
// Leaving PHP for a while ?>
<input type="text" name="smashing_sample_input" value="<?php echo
get_option( 'smashing_sample_input' ); ?>" />
<?php }
// Back to PHP
This function contains only an input field with the value of smashing_sample_input
passed through get_option(), which is what you’ll be saving. This is obviously the ID of
the settings field you created.
Finally, you need to sanitize the form input a bit. For this example, esc_attr() will be
enough, but you might want other types of sanitation:
// Sanitize
function smashing_settingsdemo_validate($input) {
// Encode
$newinput = esc_attr($input);
return $newinput;
}
So what just happened here? You created a settings page, which in turn contains settings_
fields() for all your settings fields (just one in the example — you can have more than
that) and do_settings_section() for your sections. The following is the complete
settings plugin, from start to finish:
<?php
/*
Plugin Name: Smashing Settings
Plugin URI: http://tdh.me/wordpress/smashing-settings/