admin – Settings API not saving values to database

Question

Here is what I’m attempting to do:

  • I have two fields called Client ID and Client Secret.
  • I would like to save each individual item in the database as the following keys: instagram_client_id and instagram_client_secret.

Does anyone see what I’m doing wrong with the Settings API? As you can see, I am showing the fields but when I input any value, it doesn’t create a database key or save any values.

enter image description here


So I have the following items setup:

private function create_site_admin_page()
{
    add_action('admin_menu', [$this, 'add_instagram_settings_page'], 1);
    add_action('admin_init', [$this, 'register_client_settings'], 2);
}

public function add_instagram_settings_page()
{
    add_options_page(
        'Instagram',
        'Instagram',
        'manage_options',
        'instagram',
        [
            $this,
            'render_options_page'
        ]
    );
}

public function render_options_page()
{
    // Show API Credentials fields if user is admin
    if (is_admin()) {
        $this->get_api_credentials();
    }

    echo $this->get_instagram_settings_content();
}

public function get_api_credentials()
{
    ?>
    <h2>API Credentials</h2>
    <form action="options.php" method="post">
        <?php
        settings_fields('client_section');
        do_settings_sections('instagram');
        submit_button();
        ?>
    </form>
    <?php
}

public function register_client_settings()
{
    add_settings_section(
        'client_section',
        '',
        '',
        'instagram'
    );

    // Credentials Key
    add_settings_field(
        'client_id_field',
        'Client ID',
        [$this, 'render_client_id_field'],
        'instagram',
        'client_section'
    );

    // Credentials Secret
    add_settings_field(
        'client_secret_field',
        'Client Secret',
        [$this, 'render_client_secret_field'],
        'instagram',
        'client_section'
    );

    register_setting(
        'client_section',
        'client_id_field',
        'validate_client_settings_inputs'
    );

    register_setting(
        'client_section',
        'client_secret_field',
        'validate_client_settings_inputs'
    );
}

public function validate_client_settings_inputs($input): array
{
    $output['client_id_field'] = sanitize_text_field($input['client_id_field']);
    $output['client_secret_field'] = sanitize_text_field($input['client_secret_field']);

    return $output;
}

public function render_client_id_field() {
    $client_id = get_option('instagram_client_id');
    printf(
        '<input type="text" name="%s" value="%s" />',
        esc_attr(
            'client_id'
        ),
        esc_attr(
            $client_id
        )
    );
}

public function render_client_secret_field() {
    $client_secret = get_option('instagram_client_secret');
    printf(
        '<input type="text" name="%s" value="%s" />',
        esc_attr(
            'client_secret'
        ),
        esc_attr(
            $client_secret
        )
    );
}

0
Sema 6 months 2022-02-21T21:52:53-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse