0

I've been through this code a hundred times and I just can't spot what's wrong with it.

What it does:

✓ display options page, settings section, setting fields

✓ go through the sanitation function at the end

The very last function is just supposed to check that I have inserted the values correctly, but it keeps outputting:

Array ( [jazyk] => )

This is my first time using the Settings API. Thanks for helping.

add_action( 'admin_menu', 'experimental_plugin_page' );

// tick
function experimental_plugin_page() {
 
    add_options_page(
        'Experimental', // page <title>Title</title>
        'Experimental', // menu link text
        'manage_options', // capability to access the page
        'experimental', // page URL slug
        'experimental_page_content', // callback function /w content
        5 // priority
    );
 
}

function experimental_page_content () {
    ?>
    <div class="wrap">
        <h2>My plugin</h2>
        <form action="options.php" method="post">
        <?php
        // this is what the setting is called and how you retrieve the option later! (with get_options)
        settings_fields( 'experimental_plugin_options' );
        // slug name of the page whose settings sections you want to output
        // Use this in a settings page callback function to output all the 
        // sections and fields that were added to that $page with add_settings_section() and add_settings_field()
        do_settings_sections( 'experimental' );
        submit_button( 'Save Changes', 'primary' );
        ?>
        </form>
    </div>

<?php
}

// add an action on admin init
//
//
//
add_action ('admin_init', 'experimental_plugin_admin_init');

function experimental_plugin_admin_init () {
    // a/ register settings
    $args = array(
        'type' => 'string',
        // ↓ callable
        'sanitize_callback' => 'experimental_plugin_validate_options',
        'default' => NULL
        );
    register_setting ('experimental_plugin_options', 'experimental_plugin_options', $args);

    // b/ add a settings section
    add_settings_section (
        'experimental-section-main',
        'Experimentální plugin',
        // callable ↓ - echoes anything in the section
        'experimental_plugin_section_text',
        // page name
        'experimental'
    );

    // c/ add a settings field
    add_settings_field (
        'experimental-radio-field',
        'Jazyk aktualizovaného pole',
        // ↓ callback
        'experimental_plugin_setting_jazyk',
        'experimental',
        'experimental-section-main'
    );
};
function experimental_plugin_section_text() {
    echo '<p>Nastavte jazyk.</p>';
    };

function experimental_plugin_setting_jazyk () {
    // Get option 'beast_mode' value from the database
    // Set to 'disabled' as a default if the option does not exist
    $options = get_option( 'experimental_plugin_options', [ 'jazyk' => 'EN' ] );
    $jazyk = $options['jazyk'];
    // Define the radio button options
    $items = array( 'EN', 'CZ' );
    /*$jazyk = get_option ('experimental_plugin_options');*/
    foreach( $items as $item ) {
    // Loop the two radio button options and select if set in the option value
    echo "<label><input " . checked( $jazyk, $item, false ) . "
    value='" . esc_attr( $item ) . "' name='experimental_plugin_options
    [jazyk]'
    type='radio'/> " . esc_html( $item ) . "</label><br/>";
    }
    }

    function experimental_plugin_validate_options ($input) {
        $input['jazyk'] = sanitize_text_field( $input['jazyk'] );
        return $input;
    };

    add_action('admin_init', 'justshow');
    function justshow () {
        $output = get_option('experimental_plugin_options');
        print_r($output);
    }
0

It's working. I just got rid of the $jazyk variable and made the value I was saving a simple string, not an array, which was actually all I needed.

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.