1

I'm creating a plugin that will require the user to set an API key in order to communicate with my API. My question is: where should I save it?

Logically, I would think that it should be saved in the database, and that's what I started doing it. I came up with the following code that creates a table and where I can store the api_key:

<?php

/**
 * Fired during plugin activation.
 */
class Plugin_Activator {
    public static function activate() {
        global $wpdb;

        $table_name = $wpdb->prefix . 'my-plugin-name'
        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE $table_name (
                        id int(11) NOT NULL auto_increment,
                        api_key varchar(23) NOT NULL
                ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        add_option( 'test_db_version', $test_db_version );
    }
}

BUT it feels wrong to have a table to only store this so I'm wondering if there is another way of doing it that I don't know.

1 Answer 1

1

Using the Settings API to display the field and the Options API to store the value would be a good way to go.

Your hunch is correct -- creating a custom table to store an API key is not the way to go.

Edit

Here's a complete example (copied from this oldie but goodie https://wpengineer.com/2139/adding-settings-to-an-existing-page-using-the-settings-api/) that adds a custom setting to the Options - General page:

// Register and define the settings
add_action('admin_init', 'ozhwpe_admin_init');
function ozhwpe_admin_init(){
    register_setting(
        'general',                 // settings page
        'ozhwpe_options',          // option name
        'ozhwpe_validate_options'  // validation callback
    );

    add_settings_field(
        'ozhwpe_notify_boss',      // id
        'Boss Email',              // setting title
        'ozhwpe_setting_input',    // display callback
        'general',                 // settings page
        'default'                  // settings section
    );

}

// Display and fill the form field
function ozhwpe_setting_input() {
    // get option 'boss_email' value from the database
    $options = get_option( 'ozhwpe_options' );
    $value = $options['boss_email'];

    // echo the field
    ?>
<input id='boss_email' name='ozhwpe_options[boss_email]'
 type='text' value='<?php echo esc_attr( $value ); ?>' /> Boss wants to get a mail when a post is published
    <?php
}

// Validate user input
function ozhwpe_validate_options( $input ) {
    $valid = array();
    $valid['boss_email'] = sanitize_email( $input['boss_email'] );

    // Something dirty entered? Warn user.
    if( $valid['boss_email'] != $input['boss_email'] ) {
        add_settings_error(
            'ozhwpe_boss_email',           // setting title
            'ozhwpe_texterror',            // error ID
            'Invalid email, please fix',   // error message
            'error'                        // type of message
        );
    }

    return $valid;
}

I changed the code so that the option appears on the General Options page. It won't be too difficult to modify this code to use your API key field.

3
  • 1
    Awesome! Quick question though: can I use the options API with a custom settings page? My settings page is under the options-general.php, it's not a dedicated page that shows in the wordpress sidebar.
    – mamonas
    Commented Apr 24, 2020 at 19:02
  • You sure can! I added a fully working example with a link to the article I grabbed the code from. Commented Apr 24, 2020 at 19:15
  • 1
    Hey Dave! You're amazing! Thank you very much!!!!!!!!!!!
    – mamonas
    Commented Apr 24, 2020 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.