0

I don't understand why, the custom select option works, but even when wp_dropdown_pages() is outputting the same exact HTML, it doesn't save as the select option does! I'm out of ideas :(

Here's my code:

class ThemeOptionsDashboard {
    private $theme_options_fields;

    function __construct() {
        $this->theme_options_fields = [
            'fixed_header' => [
                'type'     => 'select',
                'children' => ['Disable', 'Enable']
            ],
            'show_pages' => [
                'type'     => 'pages',
                'default'  => '',
                'sanitize' => 'pages'
             ]
        ];
    }

    function theme_options_dashboard_output() {
       ?>
        <form method="post" action="options.php" id="theme_options_form">
            <?php
                settings_fields( 'theme_options' );
            ?>
            <?php submit_button(); ?>
        </form>
    <?php
    }

    function theme_options_dashboard_validate( $input ) {
        $output = [];

        foreach ( $input as $key => $value ) {
            $field_sanitize = theme_options_field_sanitize( $key );

            switch ( $field_sanitize ) {
                case 'pages':
                    $output[ $key ] = (int) $output[ $key ];
                break;
                default:
                    $output[ $key ] = $input[ $key ];
                break;
            }
        }
        return $output;
    }

    function theme_options_field_sanitize( $key ) {
        return $this->theme_options_fields[ $key ]['sanitize'];
    }

    // no val from DB? use default
    function theme_options_default_id( $id ) {
        return $this->theme_options_fields[ $id ]['default'];
    }

    // output fields
    function theme_options_field_callback( $id ) {
        $option = get_option( 'theme_options' );
        $field_val = isset( $option[ $id ] ) ? $option[ $id ] : $this->theme_options_default_id( $id );

        switch ( $this->theme_options_fields[ $id ]['type'] ) {
            case 'select':
                echo '<select name="theme_options[' . $id . ']">';
                for ( $i = 0; $i < sizeof( $this->theme_options_fields[ $id ]['children'] ); $i++ ) {
                    echo "<option value='" . $i ."' " . selected( $field_val, $i, false ) . ">";
                    echo $this->theme_options_fields[ $id ]['children'][ $i ];
                    echo "</option>";
                }
                echo '</select>';
            break;
            case 'pages':
                wp_dropdown_pages(
                    array(
                        'name' => 'theme_options["' . $id . '"]',
                        'selected' => $field_val,
                    )
                );
            break;
        }
    }
}

new ThemeOptionsDashboard();

Your Answer

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

Browse other questions tagged or ask your own question.