1

I'm creating a settings page for a plugin. It adds:

  • a submenu item
  • registers a setting
  • adds a section
  • adds a field
  • and then renders the page/section/field.

It is all pretty straightforward, only "complexity" is when rendering the field I'm grabbing the option (an array) and comparing it to the taxonomies array returned by get_taxonomies().

The menu item, page, and section all display fine but nothing shows up for fields. I thought maybe I was getting back an empty array but I added an echo '<p>Hello!</p>' just to see if the rendering function was being called (lqdnotes_settings_render_field) - and it doesn't seem to be.

I'm sure this is a simple error but I've been staring at this code for far too long. Any suggestions greatly appreciated!

<?php
/**
 * Plugin Name: Liquid Notes Settings Page
 * Version: 0.0.1
 * Author: Liquid Church, Dave Mackey
 * License: GPLv2 or later
 * Text Domain: lqdnotes
 */

/**
 * Add Submenu Item to Settings Menu
 */
function lqdnotes_add_menu_item() {
    $page_title = 'Liquid Notes Settings';
    $menu_title = 'Liquid Notes';
    $capability = 'manage_options';
    $menu_slug = 'lqdnotes';
    $settings_page_render = 'lqdnotes_settings_render_page';

    add_options_page(
        $page_title,
        $menu_title,
        $capability,
        $menu_slug,
        $settings_page_render
    );
}

/**
 * Initialize the setting, section, and field we'll use on our Settings page.
 *
 * References:
 * - Register Setting: https://developer.wordpress.org/reference/functions/register_setting/
 * - Adding Section: https://developer.wordpress.org/reference/functions/add_settings_section/
 * - Adding Field: https://developer.wordpress.org/reference/functions/add_settings_field/
 */
function lqdnotes_settings_init() {
    // Register Setting
    $settings_option_group = 'lqdnotes_settings_taxonomies';
    $settings_option_name = 'lqdnotes_settings_taxonomies';
    // $args = array( 'type', 'description', 'sanitize_callback', 'show_in_rest', 'default' )

    register_setting(
        $settings_option_group,
        $settings_option_name
    );

    // Add Section
    $settings_slug_name = 'lqdnotes_main';
    $settings_title = 'Liquid Notes Settings';
    $settings_callback = 'lqdnotes_settings_render_section';
    $settings_page = 'lqdnotes';

    add_settings_section(
        $settings_slug_name,
        $settings_title,
        $settings_callback,
        $settings_page
    );

    // Add Field
    $settings_field_slug_name = 'lqdnotes_settings_taxonomies';
    $settings_title = 'Select taxonomies that should be associated with Liquid Notes.';
    $settings_field_callback = 'lqdnotes_settings_render_field';
    // $settings_page
    // $section = 'default'
    // $args = array( 'label_for', 'class' )

    add_settings_field(
        $settings_field_slug_name,
        $settings_title,
        $settings_field_callback,
        $settings_page
    );
}

add_action( 'admin_init', 'lqdnotes_settings_init' );

add_action( 'admin_menu', 'lqdnotes_add_menu_item' );

/**
 * Render Section
 */
function lqdnotes_settings_render_section() {
    echo '<p>Settings for Liquid Notes.</p>';
}

/**
 * Render Field
 */
function lqdnotes_settings_render_field() {
    echo '<p>Hello!</p>';
    $selected_taxonomies = get_option( 'lqdnotes_settings_taxonomies' );
    // https://developer.wordpress.org/reference/functions/get_taxonomies/
    $all_taxonomies = get_taxonomies();

    foreach ( $all_taxonomies as $taxonomy => $tax ) {
        if ( in_array( $tax, $selected_taxonomies ) ) {
            echo '<input type="checkbox" name="lqdnotes_taxonomies[]" value="' .
                $tax . '" checked="checked">' . $tax->label . '<br>';
        } else {
            echo '<input type="checkbox" name="lqdnotes_taxonomies[]" value="' . $tax->label . '<br>';
        }
    }
}

/**
 * Render Settings Page
 */
function lqdnotes_settings_render_page() {
    ?>
<div class="wrap">
    <form action="options.php" method="post">
        <?php settings_fields( 'lqdnotes_settings_taxonomies' ); ?>
        <?php do_settings_sections( 'lqdnotes' ); ?>

        <input name="Submit" type="submit" value="<?php esc_attr_e( 'Save Changes', 'lqdnotes' ); ?>"
               class="button button-primary" />
    </form>
</div>
<?php
}
1

You need to add a fifth parameter to add_settings_field() that contains the section slug, $settings_slug_name.

| improve this answer | |
  • I could swear I'd read somewhere that if there was only one section on a page then the fields would automatically be assigned to that section - I guess not! Thanks for your help! – davemackey Oct 22 '19 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.