插件设置不会保存更改

时间:2020-02-24 作者:Amol Borkar

我正在创建一个几乎没有设置的插件,我已经为它创建了一个设置页面。代码如下:

function sswc_register_settings()
{   echo "---------------------------------------------register_settings function called!";

    add_option(\'sswc_color_option\', \'#A18DC6\');
    add_option(\'sswc_top_option\', \'400\');
    add_option(\'sswc_left_option\', \'100\');

    register_setting(\'sswc_options_group\', \'sswc_color_option\');
    register_setting(\'sswc_options_group\', \'sswc_top_option\');
    register_setting(\'sswc_options_group\', \'sswc_left_option\');
}

add_action(\'admin_init\', \'sswc_register_settings\');

function sswc_register_settings_page()
{
    add_options_page(\'Social Sharing with Claps\', \'SSWC Settings\', \'manage_options\', \'sswc\', \'sswc_options_page\');
}

//Content for settings page will go here
function sswc_options_page()
{
    ?>
        <div class=\'settings-page-container\'>
            <h1>Social Sharing with Claps</h1>
            <h2>Settings</h2>

            <form method=\'post\' action=\'options-general.php?page=sswc\'>
                <?php settings_fields(\'sswc_options_page\');?>
                <table>
                    <tr valign=\'top\'>
                        <th scope=\'row\'><label class="form-label" for=\'sswc_color_option\'>Color Value</label></th>
                        <td><input type=\'text\' id=\'sswc_color_option\' name=\'sswc_color_option\' value=<?php echo get_option(\'sswc_color_option\');?>></td>
                    </tr>
                    <tr valign=\'top\'>
                        <th scope=\'row\'><label class="form-label" for=\'sswc_top_option\'>Top Offset</label></th>
                        <td><input type=\'text\' id=\'sswc_top_option\' name=\'sswc_top_option\' value=<?php echo get_option(\'sswc_top_option\');?>></td>
                    </tr>
                    <tr valign=\'top\'>
                        <th scope=\'row\'><label class="form-label" for=\'sswc_left_option\'>Left Offset</label></th>
                        <td><input type=\'text\' id=\'sswc_left_option\' name=\'sswc_left_option\' value=<?php echo get_option(\'sswc_left_option\');?>></td>
                    </tr>
                </table>
                <?php do_settings_sections( \'sswc_options_page\' ); submit_button(); ?>
            </form>

        </div>
    <?php
}

add_action(\'admin_menu\', \'sswc_register_settings_page\');
?>
问题是它不会保存我在设置页面中所做的任何更改。我怀疑是因为sswc_register_settings() 每次重置值时调用?请帮忙。

1 个回复
SO网友:HK89

---你在代码中犯了一个小错误,我在代码中解决了这个错误,所以把这个代码--

function sswc_register_settings()
    {   
        register_setting(\'sswc_options_group\', \'sswc_color_option\');
        register_setting(\'sswc_options_group\', \'sswc_top_option\');
        register_setting(\'sswc_options_group\', \'sswc_left_option\');
    }

    add_action(\'admin_menu\', \'sswc_register_settings_page\');

    function sswc_register_settings_page()
    {
        add_options_page(\'Social Sharing with Claps\', \'SSWC Settings\', \'manage_options\', \'sswc\', \'sswc_options_page\');
        add_action(\'admin_init\', \'sswc_register_settings\');
    }

    //Content for settings page will go here
    function sswc_options_page()
    {
        ?>
            <div class=\'settings-page-container\'>
                <h1>Social Sharing with Claps</h1>
                <h2>Settings</h2>

                <form method=\'post\' action=\'options.php\'>
                    <?php 
                        settings_fields(\'sswc_options_group\');
                        do_settings_sections( \'sswc_options_group\' );
                    ?>
                    <table>
                        <tr valign=\'top\'>
                            <th scope=\'row\'><label class="form-label" for=\'sswc_color_option\'>Color Value</label></th>
                            <td><input type=\'text\' id=\'sswc_color_option\' name=\'sswc_color_option\' value=<?php echo get_option(\'sswc_color_option\');?>></td>
                        </tr>
                        <tr valign=\'top\'>
                            <th scope=\'row\'><label class="form-label" for=\'sswc_top_option\'>Top Offset</label></th>
                            <td><input type=\'text\' id=\'sswc_top_option\' name=\'sswc_top_option\' value=<?php echo get_option(\'sswc_top_option\');?>></td>
                        </tr>
                        <tr valign=\'top\'>
                            <th scope=\'row\'><label class="form-label" for=\'sswc_left_option\'>Left Offset</label></th>
                            <td><input type=\'text\' id=\'sswc_left_option\' name=\'sswc_left_option\' value=<?php echo get_option(\'sswc_left_option\');?>></td>
                        </tr>
                    </table>
                    <?php  submit_button(); ?>
                </form>

            </div>
        <?php
    }
输出:

enter image description here

[请参阅此链接:]https://codex.wordpress.org/Creating_Options_Pages