Plugin settings won’t save changes

Question

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.

0
, Amol Borkar 4 years 2020-02-24T08:38:26-05:00 0 Answers 67 views 0

Leave an answer

Browse
Browse