Save WordPress plugin settings with a form

After you create your first WordPress plugin and have your WordPress plugin page setup. You want to take information typed into your plugin page and add it to the WordPress database so your plugin can read it. In this article, you’ll learn how to save WordPress plugin settings with a form.

This allows your plugin users to alter settings for your WordPress plugin and customize it to fit their needs.

extra post info admin page with non-functional form

In my example with the Extra Post Info plugin, I want to allow users to type in the extra info for their posts. Then store that information in the WordPress database so we save what they typed in.

Add form to WordPress plugin page

One of the most common things you’ll do on a WordPress plugin page is to use a form to capture some user input.

You can use the built-in WordPress options page to gather info from the user, and then save it in our WordPress database in the wp_options table. That way we can pull this info later to change what our plugin does on our site.

function extra_post_info_page(){ ?><h1>WordPress Extra Post Info</h1>
<form method="post" action="options.php">
<?php settings_fields( 'extra-post-info-settings' ); ?>
<?php do_settings_sections( 'extra-post-info-settings' ); ?>
    <table class="form-table"><tr valign="top"><th scope="row">Extra post info:</th>
    <td><input type="text" name="extra_post_info" value="<?php echo get_option( 'extra_post_info' ); ?>"/></td></tr></table>
<?php submit_button(); ?>
<?php }

Code break-down

We create a form and tell it to POST to the WordPress options.php script, so WordPress can save our data.

<form method="post" action="options.php">

We use the settings_fields function, and pass extra-post-info-settings as the group name we’d like.

<?php settings_fields( 'extra-post-info-settings' ); ?>

We use the do_settings_sections function, and pass the same group name to display our settings.

<?php do_settings_sections( 'extra-post-info-settings' ); ?>

We create a table and in the table header, we use Extra post info: for the label of our field.

<th scope="row">Extra post info:</th>

We create a text input with the name set to extra_post_info, and then we use get_option( ‘extra_post_info’ ) to display what’s already been set in the database.

<td><input type="text" name="extra_post_info" value="<?php echo get_option('extra_post_info'); ?>"/></td>

We use the WordPress submit_button() function to create a button to save our data.

<?php submit_button(); ?>

Plugin page with non-functional form

We now have enough logic in our plugin page to display a form, but it doesn’t do anything yet.

extra post info admin page with non-functional form

Register plugin settings to the WordPress database

You now want to register WordPress plugin settings to the database so that information entered into our plugin page’s form is taken and added to the database.

Thoughts on “Save WordPress plugin settings with a form

Was this article helpful? Let us know!