First of all: I am really a newbie. So I used a template for my options page and tried to customize it (works pretty good so far).
About my plugin: It creates to post types and some meta boxes inside them. Meta boxes have some fields and saving fields works pretty good so far.
Now I come to the point, where I want my options not only being displayed on the options page, but save the values entered.
I try it with a "short excerpt" cause all the other options work the same.
<?php
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_options_page( __('Nutri+', 'nutriplus' ), __('Nutri+', 'nutriplus' ), 'manage_options', 'my-plugin', 'my_options_page' );
}
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
register_setting( 'my-settings-group', 'my-plugin-settings' );
add_settings_section( 'section-1', __( 'General Settings', 'nutriplus' ), 'section_1_callback', 'my-plugin' );
add_settings_field( 'field-1-1', __( 'Enable Food Post Type', 'nutriplus' ), 'field_1_1_callback', 'my-plugin', 'section-1' );
add_settings_field( 'field-1-2', __( '- Enable Nutritional Information Meta Box', 'nutriplus' ), 'field_1_2_callback', 'my-plugin', 'section-1' );
add_settings_field( 'field-1-3', __( '- Enable Additional Information Meta Box', 'nutriplus' ), 'field_1_3_callback', 'my-plugin', 'section-1' );
add_settings_field( 'field-1-4', __( 'Enable Diet Post Type', 'nutriplus' ), 'field_1_4_callback', 'my-plugin', 'section-1' );
function my_options_page() {
?>
<div class="wrap">
<h2><?php _e('Nutri+ Options', 'nutriplus'); ?></h2>
<form action="options.php" method="POST" class="np-options">
<?php settings_fields('my-settings-group'); ?>
<?php do_settings_sections('my-plugin'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php }
function section_1_callback() {
_e( 'Here you can configure Nutri+ on a per post type base and enable/disable features.', 'nutriplus' );
}
function field_1_1_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_1";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_food_post_type' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function field_1_2_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_2";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_nutition_meta_box' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function field_1_3_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_3";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_additional_meta_box' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function field_1_4_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_4";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_diet_post_type' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function my_settings_validate_and_sanitize( $input ) {
$settings = (array) get_option( 'my-plugin-settings' );
if ( $some_condition == $input['field_1_1'] ) {
$output['field_1_1'] = $input['field_1_1'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_1', __('Error in Field "Enable Food Post Type"', 'nutriplus'), 'error' );
}
if ( $some_condition == $input['field_1_2'] ) {
$output['field_1_2'] = $input['field_1_2'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_2', __('Error in Field "Enable Nutritional Information Meta Box"', 'nutriplus'), 'error' );
}
if ( $some_condition == $input['field_1_3'] ) {
$output['field_1_3'] = $input['field_1_3'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_3', __('Error in Field "Enable Additional Information Meta Box"', 'nutriplus'), 'error' );
}
if ( $some_condition == $input['field_1_4'] ) {
$output['field_1_4'] = $input['field_1_4'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_4', __('Error in Field "Enable Diet Post Type"', 'nutriplus'), 'error' );
}
return $output;
}
Maybe, someone can help me
First of all: I am really a newbie. So I used a template for my options page and tried to customize it (works pretty good so far).
About my plugin: It creates to post types and some meta boxes inside them. Meta boxes have some fields and saving fields works pretty good so far.
Now I come to the point, where I want my options not only being displayed on the options page, but save the values entered.
I try it with a "short excerpt" cause all the other options work the same.
<?php
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_options_page( __('Nutri+', 'nutriplus' ), __('Nutri+', 'nutriplus' ), 'manage_options', 'my-plugin', 'my_options_page' );
}
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
register_setting( 'my-settings-group', 'my-plugin-settings' );
add_settings_section( 'section-1', __( 'General Settings', 'nutriplus' ), 'section_1_callback', 'my-plugin' );
add_settings_field( 'field-1-1', __( 'Enable Food Post Type', 'nutriplus' ), 'field_1_1_callback', 'my-plugin', 'section-1' );
add_settings_field( 'field-1-2', __( '- Enable Nutritional Information Meta Box', 'nutriplus' ), 'field_1_2_callback', 'my-plugin', 'section-1' );
add_settings_field( 'field-1-3', __( '- Enable Additional Information Meta Box', 'nutriplus' ), 'field_1_3_callback', 'my-plugin', 'section-1' );
add_settings_field( 'field-1-4', __( 'Enable Diet Post Type', 'nutriplus' ), 'field_1_4_callback', 'my-plugin', 'section-1' );
function my_options_page() {
?>
<div class="wrap">
<h2><?php _e('Nutri+ Options', 'nutriplus'); ?></h2>
<form action="options.php" method="POST" class="np-options">
<?php settings_fields('my-settings-group'); ?>
<?php do_settings_sections('my-plugin'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php }
function section_1_callback() {
_e( 'Here you can configure Nutri+ on a per post type base and enable/disable features.', 'nutriplus' );
}
function field_1_1_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_1";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_food_post_type' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function field_1_2_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_2";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_nutition_meta_box' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function field_1_3_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_3";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_additional_meta_box' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function field_1_4_callback() {
$settings = (array) get_option( 'my-plugin-settings' );
$field = "field_1_4";
$value = esc_attr( $settings[$field] );
echo "<input id='enable_diet_post_type' name='my-plugin-settings[$field]' type='checkbox' value='true' checked />";
}
function my_settings_validate_and_sanitize( $input ) {
$settings = (array) get_option( 'my-plugin-settings' );
if ( $some_condition == $input['field_1_1'] ) {
$output['field_1_1'] = $input['field_1_1'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_1', __('Error in Field "Enable Food Post Type"', 'nutriplus'), 'error' );
}
if ( $some_condition == $input['field_1_2'] ) {
$output['field_1_2'] = $input['field_1_2'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_2', __('Error in Field "Enable Nutritional Information Meta Box"', 'nutriplus'), 'error' );
}
if ( $some_condition == $input['field_1_3'] ) {
$output['field_1_3'] = $input['field_1_3'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_3', __('Error in Field "Enable Additional Information Meta Box"', 'nutriplus'), 'error' );
}
if ( $some_condition == $input['field_1_4'] ) {
$output['field_1_4'] = $input['field_1_4'];
} else {
add_settings_error( 'my-plugin-settings', 'invalid-field_1_4', __('Error in Field "Enable Diet Post Type"', 'nutriplus'), 'error' );
}
return $output;
}
Maybe, someone can help me
I don´t know if checkboxes need sanitze or validate…
Yes, you should sanitize. Otherwise, malicious users or codes could modify values into your database. Take a look into sanitize_option documentation.
Validation is up to you. If options must be checked for some reason (e.g.: at least 2 options checked), you should validate it and provide feedback to users.
I don´t know how to save the current value and of course load in on default. I found examples with php functions instead of value = "xxx", but don´t know how to include this
Take a look into add_option documentation to understand how to create new options (and also update them).
To load them by default, first you should get its value with get_option. Then, you should use the native checked() method provided by WordPress.
And yes… i Know… the checked attribute should not be there when the current value need to appear: )
I guess I've already answered above