I am creating my own options page for a plugin and I am having trouble getting my text field to save. My code looks like this:

function setup_fields() {
    add_settings_field('my_first_field', 'Field name', 'field_callback', 'myplugin', 'my_first_section');
add_action('admin_init', 'setup_fields');

function field_callback($arguments) {
    echo '<input name="my_first_field" id="my_first_field" type="text" value="' . get_option('my_first_field', 'default') . '" />';
    register_setting('myplugin', 'my_first_field');

When I write into the text field on my page and click "Save Changes" it always switches back to my "default." i'm a beginner so I apologize if this is a vague or basic question. Thank you!

Edit: This is the rest of my code for reference:

function myplugin_register_settings() {
    add_option('myplugin_option_name', 'Option Value');
    register_setting('myplugin_options_group', 'myplugin_option_name', 'myplugin_callback');
add_action('admin_init', 'myplugin_register_settings');

function create_plugin_settings_page() {
    add_options_page('Page Title', 'Plugin Settings', 'manage_options', 'myplugin', 'plugin_settings_page_content');
add_action('admin_menu', 'create_plugin_settings_page');

function plugin_settings_page_content() {
    <div class="wrap">
        <h2>Plugin Settings Page</h2>
        <form method="post" action="options-general.php?page=myplugin">

function setup_sections() {
    add_settings_section('my_first_section', 'First Section', 'section_callback', 'myplugin');
    add_settings_section('my_second_section', 'Second Section', 'section_callback', 'myplugin');
add_action('admin_init', 'setup_sections');

function section_callback($arguments) {
    switch($arguments['id']) {
        case 'my_first_section':
            echo 'This is my first section.';
        case 'my_second_section':
            echo 'This is my second section.';


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.