Save WordPress plugin settings with 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 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 use a form to capture some user input.

You can use the built in WordPress option 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(); ?>   </form>  <?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 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 form

  • Hi im getting error when i try to submit data and of course… i dont have a valid options.php file for processing the form. In the tutorial its not expressed what content should be in that file. What code is required to process data? Sorry for this basic questions but im totally newbie in this kind of subjects. Thank you

    • The options.php page is part of the core files for WordPress. You shouldn’t need to modify that content, you can just use the default one. The code to process the data is written in the article above. I hope this helps, please feel free to post a reply to your comment, if you have a more specific question, we may be able to assist you further.

Leave a Reply