Settings API not saving

时间:2021-04-18 作者:Kristýna Šulcová

这段代码我已经读了一百遍了,我就是看不出有什么问题。

其作用:

✓ 显示选项页,设置部分,设置字段

✓ 最后完成卫生功能

最后一个函数只需检查我是否正确插入了值,但它会不断输出:

数组([jazyk]=>;)

这是我第一次使用设置API。谢谢你的帮助。

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);
    }

1 个回复
SO网友:Kristýna Šulcová

它正在工作。我刚刚去掉了$jazyk变量,并将保存的值设为一个简单的字符串,而不是数组,这实际上是我所需要的。

相关推荐