1. Home
  2. »
  3. Wordpress Plugin Development
  4. »
  5. Plugin Development – Create a Custom WordPress Database Table

Plugin Development – Create a Custom WordPress Database Table

Plugin Development – Create a Custom WordPress Database Table

When developing a WordPress plugin, there may be instances where you need to create a custom database table to store specific data related to your plugin’s functionality. Creating a custom database table allows you to efficiently manage and retrieve data without cluttering the default WordPress database tables.

To create a custom WordPress database table in your plugin, you can use the following steps:

1. Define the database table structure:

function flashify_create_custom_table() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'custom_table';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name varchar(50) NOT NULL,
        email varchar(50) NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}
add_action('init', 'flashify_create_custom_table');

2. Insert data into the custom table:

function flashify_insert_data_into_custom_table() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'custom_table';

    $wpdb->insert(
        $table_name,
        array(
            'name' => 'John Doe',
            'email' => 'john.doe@example.com'
        )
    );
}
add_action('wp_loaded', 'flashify_insert_data_into_custom_table');

3. Retrieve data from the custom table:

function flashify_get_data_from_custom_table() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'custom_table';

    $results = $wpdb->get_results("SELECT * FROM $table_name");

    foreach ($results as $result) {
        echo 'Name: ' . $result->name . ' - Email: ' . $result->email . '<br>';
    }
}

By following these steps, you can create a custom WordPress database table in your plugin, insert data into the table, and retrieve data as needed. This allows you to efficiently manage and store specific data related to your plugin’s functionality without affecting the default WordPress database tables.

For more detailed information on creating custom database tables in WordPress plugins, you can refer to the WordPress Plugin Handbook.

Shashika De Silva

Shashika De Silva

Hey there! I’m a seasoned PHP developer with over 10 years of experience crafting awesome WordPress plugins and themes. I specialize in creating scalable and robust solutions for WordPress and WooCommerce, ensuring everything runs smoothly. Whether it’s cross-platform software development, web development, or diving into Sheets/Excel with Appscript, Macros, and VBA, I’ve got you covered. I’m all about delivering top-notch results that go beyond expectations. Let’s team up and turn your ideas into reality, making your project shine! Looking forward to working together and achieving something remarkable!

Select By Category

Flashify.Lab

Join our team
to create the best digital solutions.

Enhance your WordPress site’s functionality with custom plugins tailored to your unique needs. Our expert developers specialize in creating robust plugins that seamlessly integrate with WooCommerce, ensuring a streamlined user experience and enhanced site performance. Transform your ideas into reality with our bespoke plugin development services today

Scroll to Top