In this guide I'll walk you through creating your very first WordPress plugin. This will just be a simple plugin that adds some extra info to all our posts. Using these same steps you can add features to your own WordPress site!

What is a WordPress plugin?

A WordPress plugin simply adds additional features to a WordPress website that it wouldn't have had before.

You can look at our recommended WordPress plugins for some examples of what you can do with a plugin.

Create a WordPress plugin

Before you begin creating a WordPress plugin, you need to figure out the purpose of your plugin, and a unique name to give your plugin. This is important if you want to release your plugin to the WordPress Plugin directory.

In this guide we're going to create a plugin called WordPress Extra Post Info, and hopefully what it will accomplish is self explanatory. This plugin will let users enter in extra information for WordPress posts.

Create the plugin script

First you want to create a WordPress plugin script with some PHP code for WordPress to read.

This script needs to be uploaded to your WordPress /wp-content/plugins/extra-post-info directory.

The WordPress File Header info needs to go at the top of this script, to tell WordPress this is a plugin file.

<?php
/*
Plugin name: WordPress Extra Post Info
Plugin URI: http://example.com/wordpress-extra-post-info
Description: A simple plugin to add extra info to posts.
Author: Jacob Nicholson
Author URI: http://InMotionHosting.com
Version: 0.5
*/

?>

Create main plugin function

Next, create the WordPress plugin function that contains your plugin's logic for what the plugin will do.

To ensure your plugin uses a unique function name, you should wrap your function in a !function_exists check.

I called my function extra_post_info and have it taking the WordPress $content variable as input.

if( !function_exists("extra_post_info") )
{
  function extra_post_info($content)
  {
    $extra_info = "EXTRA INFO";
    return $content . $extra_info;
  }
add_filter('the_content', 'extra_post_info');
}

The function then returns the original $content with our own $extra_info data appended to it.

Finally it runs the WordPress add_filter hook, to actually display our returned content on the post.

Activate and test plugin

Now you can login to WordPress, click on Plugins then activate the plugin by clicking on Activate.

 

Before plugin activated

After plugin activated

 

Now as you can see our plugin successfully added the text EXTRA INFO into our WordPress post.

Create admin menu in the WordPress dashboard

To allow users to customize your plugin to fit their needs, you can create a WordPress admin menu.

This creates a icon in the WordPress dashboard that a user can click on to view plugin settings.

You define things like the $page_title to set how your plugin menu looks. You also define the $function which WordPress should use to create your plugin page that contains your settings.

Then you use the add_menu_page hook to actually create the menu and make it show up in the WordPress dashboard.

add_action( 'admin_menu', 'extra_post_info_menu' );

if( !function_exists("extra_post_info_menu") )
{
function extra_post_info_menu(){

  $page_title = 'WordPress Extra Post Info';
  $menu_title = 'Extra Post Info';
  $capability = 'manage_options';
  $menu_slug  = 'extra-post-info';
  $function   = 'extra_post_info_page';
  $icon_url   = 'dashicons-media-code';
  $position   = 4;

  add_menu_page( $page_title,
                 $menu_title, 
                 $capability, 
                 $menu_slug, 
                 $function, 
                 $icon_url, 
                 $position );
}
}

Create WordPress plugin page

Now create a WordPress plugin page so when a user clicks on your admin menu, they are presented with an options page for your plugin.

Your WordPress plugin page can just provide info to a user, or allow them to adjust the settings for how your plugin functions with their site.

if( !function_exists("extra_post_info_page") )
{
function extra_post_info_page(){
?>
  <h1>WordPress Extra Post Info</h1>
<?php
}
}
?>

Save WordPress plugin settings with a form

Now you can save WordPress plugin settings with a form so a user's settings are kept for later use.

if( !function_exists("extra_post_info_page") )
{
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
}
}

Register WordPress plugin settings to the database

Now you can register WordPress plugin settings to the database, so a user's settings are kept for later use.

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

Read WordPress plugin settings from the database

Finally, bring your plugin development full circle when you read WordPress plugin settings from the database.

if( !function_exists("extra_post_info") )
{

  function extra_post_info($content)
  {
    $extra_info = get_option('extra_post_info');
    return $content . $extra_info;
  }

add_filter('the_content', 'extra_post_info');

}

Completed WordPress plugin

With the WordPress plugin we've created, a user can update the info they want displayed on all posts from the dashboard. This is a very simple plugin, and just scratches the surface of the power of WordPress plugins.

You can view the full complete Extra Post Info WordPress plugin if you got lost during any of the steps.

extra post info admin page read setting from 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!