InMotion Hosting Support Center

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>
    <?php submit_button(); ?>


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.

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Support Center Login

Our Login page has moved, Click the button below to be taken to the login page.

n/a Points
2018-02-12 10:26 am

Thank you man! You're awesome. This tutorial is really nice :)

2,032 Points
2018-02-12 1:08 pm
You are welcome! We are glad you enjoyed it.
n/a Points
2017-01-16 9:42 am

How can I avoid to save changes if it's not validated?

13,821 Points
2017-01-16 9:53 am
Validation has been added to WordPress core since 4.6. Here's the link to the explanation:

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

4 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!