How to write a simple plugin for WordPress

How to write a simple plugin for WordPress

Hello! Although I don’t have much experience creating plugins, in this article I’d like to share an example of building a simple plugin that will:

  1. Store data in the database via the admin panel.
  2. Display that data on the front end for site visitors.

First — create a folder in /wp-content/plugins/ for the plugin. Let’s name it my-plugin.

Second — inside that folder, add a PHP file that handles the plugin logic. I usually name it the same as the folder — in this case, my-plugin.php.

Third. To make WordPress recognize this file as a plugin, you need to add metadata at the top of the file (plugin name, description, author, etc.). For example:

/*
Plugin Name: My Plugin
Plugin URI: http://example.com
Description: My Plugin Description
Version: 1.0
Author: My Name
Author URI: http://example.com
License: GPL2
*/

Plugin Name — the plugin’s name
Plugin URI — plugin page
Description — short description
Version — version number
Author — plugin author
Author URI — author website
License — license type

Back-end (admin panel)

To access the plugin’s page with a data-saving form, we first need to register a menu link in the admin panel. You can place it inside an existing menu or as a standalone item — even a multi-level menu like “Settings” or “Posts”.

Use the admin_menu hook like this:

/* Add menu items to the admin menu */
function myp_admin_action()
{
/* Add a menu sub-item to the "Settings" section */
add_options_page('My plugin (title)', 'My plugin (menu item)', 'manage_options', __FILE__, 'myp_admin_page_options');
}
/* Register a function for creating a menu */
add_action('admin_menu', 'myp_admin_action');

The add_options_page function accepts 5 arguments:

  • page title;
  • menu label;
  • capability required (e.g., manage_options);
  • unique menu slug;
  • callback function that outputs the page content.

In our case, it’s myp_admin_page_options:

function myp_admin_page_options()
{
?> 
<div class="wrap"> 
<h2><?php echo __('My Plugin settings', 'myp_domain')?></h2> 
<form method="post" action="options.php"> 
<?php wp_nonce_field('update-options'); ?> 
<input type="hidden" name="action" value="update" /> 
<input type="hidden" name="page_options" value="myp_param1,myp_param2" /> 
<p> 
<?php echo __('Option 1:', 'myp_domain'); ?> 
<input type="text" name="myp_param1" value="<?php echo get_option('myp_param1'); ?>" size="20"> 
</p> 
<p> 
<?php echo __('Option 2:', 'myp_domain'); ?> 
<input type="text" name="myp_param2" value="<?php echo get_option('myp_param2'); ?>" size="20"> 
</p> 
<?php submit_button(); ?> 
</form> 
</div>
<?php
}

This function creates the settings form:

Plugin form example

Now let’s go over key details.

Nonce field for form validation:

wp_nonce_field('update-options');

Hidden input to trigger update:

<input type="hidden" name="action" value="update" />

Which fields to save:

<input type="hidden" name="page_options" value="myp_param1,myp_param2" />

Input fields for options:

<p> 
<?php echo __('Option 1:', 'myp_domain'); ?> 
<input type="text" name="myp_param1" value="<?php echo get_option('myp_param1'); ?>" size="20">
</p>
<p> 
<?php echo __('Option 2:', 'myp_domain'); ?> 
<input type="text" name="myp_param2" value="<?php echo get_option('myp_param2'); ?>" size="20">
</p>

To fetch data from the DB:

get_option('myp_param1');

Submit button:

submit_button();

Finally, developers often forget to clean up after uninstall. Here's how to delete options on deactivation (optional):

register_deactivation_hook(__FILE__, 'myp_deactivate');

function myp_deactivate()
{
	delete_option('myp_param1');
	delete_option('myp_param2');
}

Front-end (what visitors see)

To output saved values in your theme template:

<?php echo get_option('myp_param1'); ?>
/*or*/
<?php echo get_option('myp_param2'); ?>

That’s all!

Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *