$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - How to handel multiple checkbox field in the admin settings page with Settings API|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - How to handel multiple checkbox field in the admin settings page with Settings API

matteradmin9PV0评论

I'm trying to incorporate a list of checkbox in my plugin's admin settings page from which users can select a few countries from the list of all countries.

So far I've done this:

add_action( 'admin_init', 'register_page_options' );

function register_page_options() {
    // Add Section for option fields
    add_settings_section( 'aicp_section', __( '....text here....', 'aicp' ), 'display_section', 'aicp_settings' ); // id, title, display cb, page

    // Add Field for selecting countries for which you wanna ban ads
    add_settings_field( 'aicp_country_list', __( 'Select the countries', 'aicp' ), 'country_list_field', 'aicp_settings', 'aicp_section' ); // id, title, display cb, page, section

    // Register Settings
    register_setting( 'aicp_settings', 'aicp_settings_options', array( $this, 'validate_options' ) );
}

// now comes the section for checkbox

function country_list_field() {
    $options = get_option( 'aicp_settings_options' );
    ?>
    <input type="checkbox" name="aicp_settings_options[country_list][]" value="AF"<?php checked( 'AF' == $options['country_list'] ); ?> /> Afganistan
    <input type="checkbox" name="aicp_settings_options[country_list][]" value="AX"<?php checked( 'AX' == $options['country_list'] ); ?> /> Aland Islands
    <?php
}

Please Note: This is not the entire code, but a short part of it to give you guys the idea about my question.

Now as you can see I'm using checkboxes above I want all the selected options to be stored as comma seperated, like AF,AX,IN,US etc. so that when I need to work with those data, I can just simply explode them and use.

Anyways after a bit digging I found this answer: How to use checkbox and radio button in options page? which has showed how to handel checkbox & radio box in the settings api.

But the problem is that as I'm using the checkbox for a country list, I can't just simply used the checked() to see which of the checkbox are selected as there are a ton of country name and the user can select any one of them or all of them or some of them.

Also when I checked this link: it showed me to use [] with the name for the checkbox so that they can be stored as array.

Now as I'm using the WP Settings API I'm already using naming like aicp_settings_options[country_list] which itself is an array. So, should I have to create a 2 dimentional array like this: aicp_settings_options[country_list][] ?

I'm really confused about how should I grab the data and store them. Also how can I easily check which of the checkboxes are selected by the user.

It would be great if anyone can help. Thank you in advance.

I'm trying to incorporate a list of checkbox in my plugin's admin settings page from which users can select a few countries from the list of all countries.

So far I've done this:

add_action( 'admin_init', 'register_page_options' );

function register_page_options() {
    // Add Section for option fields
    add_settings_section( 'aicp_section', __( '....text here....', 'aicp' ), 'display_section', 'aicp_settings' ); // id, title, display cb, page

    // Add Field for selecting countries for which you wanna ban ads
    add_settings_field( 'aicp_country_list', __( 'Select the countries', 'aicp' ), 'country_list_field', 'aicp_settings', 'aicp_section' ); // id, title, display cb, page, section

    // Register Settings
    register_setting( 'aicp_settings', 'aicp_settings_options', array( $this, 'validate_options' ) );
}

// now comes the section for checkbox

function country_list_field() {
    $options = get_option( 'aicp_settings_options' );
    ?>
    <input type="checkbox" name="aicp_settings_options[country_list][]" value="AF"<?php checked( 'AF' == $options['country_list'] ); ?> /> Afganistan
    <input type="checkbox" name="aicp_settings_options[country_list][]" value="AX"<?php checked( 'AX' == $options['country_list'] ); ?> /> Aland Islands
    <?php
}

Please Note: This is not the entire code, but a short part of it to give you guys the idea about my question.

Now as you can see I'm using checkboxes above I want all the selected options to be stored as comma seperated, like AF,AX,IN,US etc. so that when I need to work with those data, I can just simply explode them and use.

Anyways after a bit digging I found this answer: How to use checkbox and radio button in options page? which has showed how to handel checkbox & radio box in the settings api.

But the problem is that as I'm using the checkbox for a country list, I can't just simply used the checked() to see which of the checkbox are selected as there are a ton of country name and the user can select any one of them or all of them or some of them.

Also when I checked this link: https://stackoverflow/questions/6881039/how-to-handle-multiple-checkboxes-in-a-php-form it showed me to use [] with the name for the checkbox so that they can be stored as array.

Now as I'm using the WP Settings API I'm already using naming like aicp_settings_options[country_list] which itself is an array. So, should I have to create a 2 dimentional array like this: aicp_settings_options[country_list][] ?

I'm really confused about how should I grab the data and store them. Also how can I easily check which of the checkboxes are selected by the user.

It would be great if anyone can help. Thank you in advance.

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked Oct 29, 2016 at 12:37 iSaumyaiSaumya 93619 silver badges36 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4
add_action( 'admin_init', 'register_page_options' );

function register_page_options() {
    if (false == get_option('aicp_settings_options')) {
        add_option('aicp_settings_options');
    }

    // Add Section for option fields
    add_settings_section( 'aicp_section', __( '....text here....', 'aicp' ), 'display_section', 'aicp_settings' ); // id, title, display cb, page

    // Add Field for selecting countries for which you wanna ban ads
    add_settings_field( 'aicp_country_list', __( 'Select the countries', 'aicp' ), 'country_list_field', 'aicp_settings', 'aicp_section' ); // id, title, display cb, page, section

    // Register Settings
    register_setting( 'aicp_settings', 'aicp_settings_options', array( $this, 'validate_options' ) );
}

// now comes the section for checkbox

function country_list_field() {
    $options = get_option( 'aicp_settings_options' );

    $value = array();
    if (isset($options['country_list']) && ! empty($options['country_list'])) {
        $value = $options['country_list'];
    }

    $html = '<input type="checkbox" name="aicp_settings_options[country_list][]" value="AF"'. in_array('AF', $value) ? 'checked' : '' .'/> Afganistan';
    $html .= '<input type="checkbox" name="aicp_settings_options[country_list][]" value="AX"'. in_array('AX', $value) ? 'checked' : '' .'/> Aland Islands';

    echo $html;
}

Instead of Using checked() you can make use of in_array() as mentioned below -

<input type="checkbox" name="aicp_settings_options[country_list][]" value="AF"<?php echo in_array('AF', $options['country_list']) ? 'checked' : ''; ?> /> Afganistan

I hope this helps.

Post a comment

comment list (0)

  1. No comments so far