admin管理员组

文章数量:1391987

I'm creating a plugin which have few settings, I've created a settings page for it. Here's the code:

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');
?>

The problem is that it won't save any changes I make in settings page. I'm suspicious its because the sswc_register_settings() get called everytime an resets the values? Please help.

I'm creating a plugin which have few settings, I've created a settings page for it. Here's the code:

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');
?>

The problem is that it won't save any changes I make in settings page. I'm suspicious its because the sswc_register_settings() get called everytime an resets the values? Please help.

Share Improve this question asked Feb 24, 2020 at 6:32 Amol BorkarAmol Borkar 1213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

--- You made a little mistake in code , I resolve this Error in the code So put this code ---

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
    }

Output :

[Refer This Link :] https://codex.wordpress/Creating_Options_Pages

本文标签: wp adminPlugin settings won39t save changes