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.

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.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?

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

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

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

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

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: support@InMotionHosting.com
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!