Support » Plugins » Hacks » Plugin options page: grouping checkboxes

  • Resolved Ethan Jinks O’Sullivan

    (@ethanosullivan)


    I’ve been using the WordPress Option Page Generator by Jeremy Hixon to help give me a head start with my plugin’s option page. I’ve laid out four different types of checkboxes which the source code can be found here:

    https://gist.github.com/factmaven/3896a3aac3888aa5bcddae48d9da5238

    However, I’d like to add additional checkboxes in each of the four section that I’ve made, like so:

    http://i.stack.imgur.com/jlxOT.jpg

    Based on the generator, it looks like I would need to add another add_settings_field and another callback function to add more checkboxes; but that looks more tedious than I’d imagine just to add more checkboxes under each section. Is it possible to add additional checkboxes underneath the add_settings_field that I’ve already created? For example:

    public function personal_options_0_callback() {
        printf(
            '<input type="checkbox" name="profile_settings_option_name[personal_options_0]" id="personal_options_0" value="personal_options_0" %s> <label for="personal_options_0"> Visual Editor</label>',
            ( isset( $this->profile_settings_options['personal_options_0'] ) && $this->profile_settings_options['personal_options_0'] === 'personal_options_0' ) ? 'checked' : ''
        );
        printf(
            '<input type="checkbox" name="profile_settings_option_name[personal_options_1]" id="personal_options_1" value="personal_options_1" %s> <label for="personal_options_1"> Admin Color Scheme</label>',
            ( isset( $this->profile_settings_options['personal_options_1'] ) && $this->profile_settings_options['personal_options_1'] === 'personal_options_1' ) ? 'checked' : ''
        );
    }

    Any help or guidance is appreciated. I’ve never made a plugin options page before, so if there is a simpler way of doing this, I am also open to your recommendation.

    If you’re on the WordPress Stack Exchange, I’ve also made the topic there if it’s better to respond there:

    https://wordpress.stackexchange.com/questions/235267/plugin-options-page-grouping-checkboxes

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, in general terms the Settings API is more tedious than seems necessary. You can add more fields by simply printing out the HTML, but then you lose the advantage the API gives you in automatically saving settings to the options table. Any fields added without using the API will need additional code to save those settings when the form is submitted.

    While it’s still tedious, what some devs do is create a settings definition array, or even a CDF like data stream. The data is then looped or stepped through, calling add_settings_field() each time, plugging in the parameters from the initial definition data each time. Somehow the array or CDF format seems less tedious than individual add_settings_field() calls.

    IMO, the Settings API is most useful when adding a few settings to an existing page. For full, unique options pages, I prefer to code my own options page and forget the Settings API altogether. It does mean needing to format my own content and process the form submit myself, but I find it preferable to the tedium of the Settings API. Additionally, I can efficiently store the various options in a single array when it’s appropriate, or even store things elsewhere besides the options table.

    Thank you for your input. From my research, it seems that there are many developers using their own framework for plugin settings pages, which is difficult to find a helpful guide or template to base off of other than the Settings API.

    What does CDF mean?

    After doing a quick search of a settings definition array, I was able to find this article which is helpful, but still looks like it requires more effort than it should.

    As someone who disregards the Settings API, are there resources to help me with my own DIY (do-it-yourself) for my plugin settings page?

    I know that the settings page in HTML, at minimum, requires the following:

    <div class="wrap">
        <h2>Header Title</h2>
        <p>Some description of the page here...</p>
    
        <form method="post" action="action-here.php" >
            <?php submit_button(); ?>
        </form>
    </div>
    Moderator bcworkz

    (@bcworkz)

    What’s CDF? Apparently something I made up due to TMA (too many acronyms). I’ve conflated CSV (comma separated values) with PDF (portable document format). I meant CSV, you know, like this:

    'personal_options_0', 'Personal Options', 'personal_options_0_callback', 'profile-settings-admin', 'profile_settings_setting_section'
    'name_1', 'Name', 'name_1_callback', 'profile-settings-admin',  'profile_settings_setting_section'

    Sorry for the confusion, I guess I should have had more coffee yesterday!

    I regret I cannot provide any useful reference for producing extra-API settings pages. I typically just output my settings using the same table format that native settings pages use. I submit the form via AJAX because linking to a menu page callback function doesn’t work very well 🙂 It’s not all that simple and straight forward either. I sometimes think the Settings API may not be all that bad, but when I start coding up each field for the API, I remember how much I hate that and go beck to my manually created page.

    I see. After some testing, I found it easier to just create my own settings page. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Plugin options page: grouping checkboxes’ is closed to new replies.