settings_fields doesn’t appear to be running

Question

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 returned by get_taxonomies()

The menu item, page, and section all display 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 function to render the fields was being called – 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.
 *
 * Reference on Register Setting: https://developer.wordpress.org/reference/functions/register_setting/
 * Reference on Adding Section: https://developer.wordpress.org/reference/functions/add_settings_section/
 * Reference on 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
}
0
, , davemackey 2 years 2019-10-21T13:34:06-05:00 0 Answers 73 views 0

Leave an answer

Browse
Browse