How to Create a WordPress Plugin

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: Description: A simple plugin to add extra info to posts. Author: Jacob Nicholson Author URI: 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

Thoughts on “How to Create a WordPress Plugin

  • “ERROR: options page not found”

    It is because this article misses the line which would register the function that is supposed to upload the data to the DB.


    See, at the “Create admin menu in the WordPress dashboard” section.

    The bottom of ‘extra_post_info_menu()’ should look like this:


      add_menu_page( $page_title,
                     $position );
      // Call update_extra_post_info function to update database
      add_action( 'admin_init', 'update_extra_post_info' );

    Very nice article BTW.
  • Hello! This is an awesome tutorial and I followed the instructions and copied the code but for some reason I keep getting ERROR: options page not found when I hit submit. Is there a solution to this? I keep getting infomation on whitelisting it but I dont know where to put it. Can you please help me? 

    • Thanks for the feedback! Sorry to see that you are experiencing an error with this. I’m unaware of any solution to the error you are receiving, in my experience, I haven’t run into that error. However, I did review online and see there was a solution posted here that may help you further.

Was this article helpful? Let us know!