How to save Checkbox-Options in Plugin Options Page

Question

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;
}
  1. I don´t know if checkboxes need sanitze or validate…
  2. 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
  3. And yes… i Know… the checked attribute should not be there when the current value need to appear: )

Maybe, someone can help me

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/how-to-save-checkbox-options-in-plugin-options-page/">

How to save Checkbox-Options in Plugin Options Page</a>
0
, , , BuddyHoli 3 months 0 Answers 34 views 0

Leave an answer