Skip to main content
deleted 7 characters in body
Source Link
mamonas
  • 123
  • 4

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 a little out 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.

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.

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 a little out 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.

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.

Source Link
mamonas
  • 123
  • 4

Where should I save an API key of my user when installing my plugin?

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 a little out 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.