0

I've been searching around for an answer to this one but have had no luck thus far… forgive me if it has been answered somewhere before!

I'm currently writing a plugin and I'm using the WordPress Plugin Boilerplate for the first time. I am also, believe it or not, utilising a select field in the admin settings page of said plugin for the first time as well.

The problem that I am running into is that when I save my changes, while all my input fields return their saved values, the select field does not.

Here is where I am creating the select field function to be reused across the plugin:

/**
 * Creates a select field.
 *
 * @since       1.0.0
 */
public function field_select( $args ) {

    $defaults['aria']           = '';
    $defaults['blank']          = '';
    $defaults['class']          = '';
    $defaults['context']        = '';
    $defaults['id']             = '';
    $defaults['description']    = '';
    $defaults['label']          = '';
    $defaults['name']           = $this->plugin_name . '-options[' . $args['id'] . ']';
    $defaults['selections']     = array();
    $defaults['value']          = '';

    apply_filters( $this->plugin_name . '-field-select-options-defaults', $defaults );

    $atts = wp_parse_args( $args, $defaults );

    if ( ! empty( $this->options[$atts['id']] ) ) {

        $atts['value'] = $this->options[$atts['id']];

    }

    if ( empty( $atts['aria'] ) && ! empty( $atts['description'] ) ) {

        $atts['aria'] = $atts['description'];

    } elseif ( empty( $atts['aria'] ) && ! empty( $atts['label'] ) ) {

        $atts['aria'] = $atts['label'];

    }

    include( plugin_dir_path( __FILE__ ) . 'partials/' . $this->plugin_name . '-admin-field-select.php' );

}

And here is where I am adding a settings field, using add_settings_field:

add_settings_field(
        'background',
        'Background Colour',
        array( $this, 'field_select_text' ),
        $this->plugin_name . '-customise',
        $this->plugin_name . '-customise',
        array(
            'name'              => 'background',
            'id'                => 'background',
            'selections'        => array(
                0 => array(
                    'label' => 'Brand',
                    'value' => 'brand'
                ),
                1 => array(
                    'label' => 'Transparent',
                    'value' => 'transparent'
                ),
                2 => array(
                    'label' => 'Custom',
                    'value' => 'custom'
                )
            )
        )
    );

And finally, here is the markup for the select field:

<select aria-label="<?php esc_attr( $atts['aria'] ); ?>"
    class="<?php echo esc_attr( $atts['class'] ); ?>"
    id="<?php echo esc_attr( $atts['id'] ); ?>"
    name="<?php echo esc_attr( $atts['name'] ); ?>">

<?php
if ( ! empty( $atts['blank'] ) ) {
    ?>

    <option value><?php esc_html_e( $atts['blank'], $this->plugin_name ); ?></option>

    <?php
}

foreach ( $atts['selections'] as $selection ) {

    if ( is_array( $selection ) ) {

        $label = $selection['label'];
        $value = $selection['value'];

    } else {

        $label = strtolower( $selection );
        $value = strtolower( $selection );

    }
    ?>

    <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $atts['value'], $value ); ?>>
        <?php esc_html_e( $label, $this->plugin_name ); ?>
    </option>

    <?php
}
?>

Does anybody have any ideas at all? It's probably something obvious with a simple solution, and I'll surely kick myself. But please. HELP. ME.

0

Oh I'm kicking myself. The select fields weren't saving because for some bizarre reason, at some point when I was working on this, their names weren't formatted correctly, i.e. background instead of plugin-name[background]. Such a simple solution to an obvious error!

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.