How to write a WordPress plugin?

WordPress is the most popular CMS, and it is also evolving as an application framework. With plugins, we can extend 🏹 its core functionalities and adapt it to our particular purposes🎯.

In this tutorial, we are going to build a small plugin to remove unused menu items from the dashboard.

screenshots

Where the plugins live?

by default we have 2 plugins installed

we can find the code of them under the path “my-website\wp-content\plugins

if we check the “hello.php” one(which is the first plugin ever developed for WordPress), we can get a pretty clear idea about the structure of a plugin file.

The header

<?php
/**
 * @package Hello_Dolly
 * @version 1.7.2
 */
/*
Plugin Name: Hello Dolly
Plugin URI: http://wordpress.org/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.7.2
Author URI: http://ma.tt/
*/

this header give the display for the plugin data and information on the plugins menu

let’s copy the file “hello.php”, to “myplugin.php” for example, and only keep the header, changing the information, to our new plugin, for example:

/**
 * @package My_Awesome_Plugin
 * @version 0.0.1
 */
/*
Plugin Name: My Awesome Plugin
Description: This plugin will add awesome functionalities to your website!
Author: Mahfoudh Arous
Version: 0.0.1
Author URI: http://aladinstudio.com/
*/

and we will get this in the plugins panel

Adding Code

Introducing WordPress Functions

WordPress functions offer a quick ⚡️ and powerful 💪 way to handle tasks within the system.

Here we going to use this function

Introducing WordPress Hooks

This is where the magic happens ✨, we can anchor any code to a hook to trigger it at a particular event.

In this example, the hook “admin_menu” when loading the admin menu, will trigger our function “remove_admin_menu

Let’s adjust our information in the header, and write the following code

/*
Plugin Name: Custom Admin Menus
Description: This my first plugin.
Author: Mahfoudh Arous
Version: 0.1
Author URI: https://aladindev.com
*/

function remove_admin_menus(){
	 //remove_menu_page( 'index.php' );                  //Dashboard
	 //remove_menu_page( 'jetpack' );                    //Jetpack* 
	 remove_menu_page( 'edit.php' );                   //Posts
	 //remove_menu_page( 'upload.php' );                 //Media
	 remove_menu_page( 'edit.php?post_type=page' );    //Pages
	 //remove_menu_page( 'edit-comments.php' );          //Comments
	 remove_menu_page( 'themes.php' );                 //Appearance
	 //remove_menu_page( 'plugins.php' );                //Plugins
	 remove_menu_page( 'users.php' );                  //Users
	 //remove_menu_page( 'tools.php' );                  //Tools
	 //remove_menu_page( 'options-general.php' );        //Settings
   }

add_action( 'admin_menu', 'remove_admin_menus' );

Now, when activating our plugin, the un-commented lines will remove the related menu page. So we got our plugin working 🎉🎉🎉

Building the Plugin Page in the Dashboard

The Options Form

To give the user the possibility to choose visually which menu he wants to display or to remove, we need to make an interface for the user by adding this function inside our plugin

function ram_options_page() {
	?>
		<div class="wrap">
			<?php screen_icon(); ?>
			<h2><?php _e( 'Remove Admin Menus', 'ram' ); ?></h2>
			<p><?php _e( 'This plugin helps you remove admin menus.', 'ram' ); ?></p>
			<form method="POST" action="options.php">
				<?php
					settings_fields( 'ramdev_settings' );
				?>
				<table class="form-table">
					<tbody>
						<?php ramdev_do_options(); ?>
					</tbody>
				</table>
				<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'ps-demo' ); ?>" />
				<input type="hidden" name="ps-plugindev-submit" value="Y" />
			</form>
		</div>
	<?php
}

Register the Plugin Settings

To store our plugin settings in the database, we need to use “register_setting” and the hook “admin_init

function ramdev_init() {
	register_setting( 'ramdev_settings', 'ramdev', 'ramdev_validate' );
}
add_action( 'admin_init', 'ramdev_init' );

to read the settings and display the previously chosen ones as checked, we will use the function:

and create our function as below

function ramdev_do_options() {
	$options = get_option( 'ramdev' );
	console_log("reading options");
	console_log($options);

	ob_start();
	?>
		<tr valign="top"><th scope="row"><?php _e( 'What menus do you want to remove?', 'ram' ); ?></th>
			<td>
				<?php
					foreach ( ramdev_services() as $service ) {
						$label = $service['label'];
						$value = $service['value'];
						echo '<label><input type="checkbox" name="ramdev[' . $value . '] value="1" ';
						switch ($value) {
							case 'appearence' :
								if ( isset( $options['appearence'] ) ) { checked( 'on', $options['appearence'] ); }
								break;
						}
						echo '/> ' . esc_attr($label) . '</label><br />';
					}
				?>
			</td>
	<?php
}

we will wrap everything up and make an entry point under the settings menu

function ramdev_menu() {
	add_submenu_page( 'options-general.php', __( 'Remove Admin Menus', 'ram' ), __( 'Remove Admin Menus', 'ram' ), 'administrator', 'ram_dev', 'ram_options_page' );
}
add_action( 'admin_menu', 'ramdev_menu' );

Conclusion

To build a plugin, we need to know the right WordPress functions and hooks. Then of course building a page and put an entry point to it, in the dashboard menu.

Finally, You can check or download the full working code for this plugin here:

https://github.com/aladin002dz/my-first-wordpress-plugin

Leave a Reply

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