Register WordPress plugin settings to database

When you create a WordPress plugin, and you’re already saving WordPress plugin settings with a form. You want to register those settings and store them in the database with WordPress, so it can change your site.

Register plugin settings with WordPress

We want to take the info entered by a plugin user, and store it into the WordPress database. To do this we need to register our setting changes with WordPress so it can store this information in the wp_options table.

Add add_action hook to register settings

When you create a WordPress admin menu you can add an add_action function after the add_menu_page function. This call another function update_extra_post_info to save info to the database from our form.

 add_menu_page( $page_title,                  $menu_title, $capability, $menu_slug, $function,                   $icon_url, $position );
add_action( 'admin_init', 'update_extra_post_info' ); }

Register settings with register_setting function

Creating a function called update_extra_post_info you can use the WordPress register_setting function to store info from our form into the database. The value will be stored next to extra_post_info in the wp_options table.

if( !function_exists("update_extra_post_info") ) { 
function update_extra_post_info() {   register_setting( 'extra-post-info-settings', 'extra_post_info' ); } }

Plugin settings now saved to WordPress database

When you view your plugin page, you can see that when EXTRA INFO is entered into our settings, it ends up in the WordPress database beside extra_post_info. When this info is updated, so is the database entry.

extra post info admin page register setting in database

Read WordPress plugin settings from database

With your plugin settings saved in the database, the next step is to read WordPress plugin settings from the database to complete your plugin.

Thoughts on “Register WordPress plugin settings to database

Was this article helpful? Let us know!