Jul 28 2017
Jul 28, 2017

How to build a Wordpress Plugin

Categorised in:

Hello! Welcome to another tutorial! Today we will build a Wordpress plugin. 

This plugin will remove the Wordpress News meta box from the dashboard. There is a video tutorial for this article. You can find it here.

To get started let's go to our plugins directory which is located in wp-content/plugins/.

In this folder we will create another folder and we will name it my-plugin. Open it and create a php file my-plugin.php and let’s paste there the following code:

<?php
/*
Plugin Name: My first plugin
PLugin URI: http://ruvictor.com
Description: This plugin will remove the meta box!
Author: Victor Rusu
Author URI: http://ruvictor.com
Version: 1
*/

So, you my say: “This is just a PHP comment”. Yes, it is a PHP comment but it is very important because Wordpress reads it. All the information from this PHP comment will appear on your dashboard where you have all your installed plugins.

The next step would be the function which will remove our meta box. Let’s continue with this code:

function victor_remove_metabox(){
	remove_meta_box('dashboard_primary','dashboard','side');
}
add_action('wp_dashboard_setup', 'victor_remove_metabox');

Here we have two predefined functions by Wordpress which are remove_meta_box() and add_action().

remove_meta_box() - Removes a meta box or any other element from a particular post edit screen of a given post type. It can be also used to remove a widget from the dashboard screen.

add_action() - Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Plugins can specify that one or more of its PHP functions are executed at these points, using the Action API.

To read more information about these functions I recommend WordPress Codex, the online manual for WordPress and a living repository for WordPress information and documentation.

Alright! The final my-plugin.php file should look like this:

<?php
/*
Plugin Name: My first plugin
PLugin URI: http://ruvictor.com
Description: This plugin will remove the meta box!
Author: Victor Rusu
Author URI: http://ruvictor.com
Version: 1
*/

function victor_remove_metabox(){
	remove_meta_box('dashboard_primary','dashboard','side');
}
add_action('wp_dashboard_setup', 'victor_remove_metabox');
?>

Let’s save this file and let’s open the dashboard using the browser. Go to Installed Plugins and there you should see your Plugin. Activate it and if everything is correct in your code the Meta Box Events and News should disappear from the dashboard.