WordPress settings API

There are three main components in the Settings API: The Setting, the Field and the Section. The Setting is the actual option that is stored inside the WordPress database. The Field is a form control rendered in our options page, such as a text input, or a dropdown select. The Section is just a way to group fields together.

<?php
function antispampro_menu() { // add menu item
	add_options_page( 'Anti-spam-pro', 'Anti-spam-pro', 'manage_options', 'anti-spam-pro', 'antispampro_options' );
}
add_action( 'admin_menu', 'antispampro_menu' );
 
function antispampro_admin_init() {
	register_setting( 'antispampro-options-group', 'antispampro_options'/*, 'antispampro_options_validate'*/ );
	add_settings_section( 'antispampro-options-section', 'Section One', 'antispampro_section_callback', 'antispampro-plugin' );
	add_settings_field( 'send_spam_comment_to_email', 'Send spam comment to email', 'antispampro_field_send_spam_to_email_callback', 'antispampro-plugin', 'antispampro-options-section' );
	add_settings_field( 'email', 'Email', 'antispampro_field_email_callback', 'antispampro-plugin', 'antispampro-options-section' );
 
}
add_action( 'admin_init', 'antispampro_admin_init' );
 
function antispampro_default_options() {
	$options = array(
		'send_spam_comment_to_email' => '1',
		'email' => 'info@mail.com'
	);
	return $options;
}
 
function antispampro_options_init() { // set options equal to defaults
	global $antispampro_options;
	$antispampro_options = get_option( 'antispampro_options' );
	if ( $antispampro_options === false ) {
		$antispampro_options = antispampro_default_options();
	}
	update_option( 'antispampro_options', $antispampro_options );
}
add_action( 'admin_init', 'antispampro_options_init' );
 
function antispampro_options_validate( $input ) {
	$output = get_option( 'antispampro_options' );
 
	//if ( is_email( $input['email'] ) )
	//	$output['email'] = $input['email'];
	//else
	//	add_settings_error( 'my-settings', 'invalid-email', 'You have entered an invalid e-mail address.' );
 
	return $output;
}
 
function antispampro_section_callback() {
	echo 'Anti-spam-pro options description.';
}
 
function antispampro_field_email_callback() {
	$option = (array) get_option( 'antispampro_options' );
	echo '<input type="text" name="antispampro_options[email]" value="'.esc_attr($option['email']).'" />';
}
 
function antispampro_field_send_spam_to_email_callback() {
	$option = (array) get_option( 'antispampro_options' );
	echo '<input type="text" name="antispampro_options[send_spam_comment_to_email]" value="'.esc_attr($option['send_spam_comment_to_email']).'" />';
}
 
function antispampro_options() {
	?>
	<div class="wrap">
		<h2>Anti-spam-pro Options</h2>
		<form method="post" action="options.php">
			<?php settings_fields( 'antispampro-options-group' ); ?>
			<?php do_settings_sections( 'antispampro-plugin' ); ?>
			<?php submit_button(); ?>
		</form>
	</div>
	<?php
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *