WordPress Plugin Tutorial – How to Write First Plugin?


WordPress is so powerful because you can write plugins for it. Plugins are useful to manage and won’t be affected if you upgrade/switch themes, or upgrading wordpress cores. Writing a plugin for wordpress is easy.

Plugin Folder

WordPress plugins are stored in /wp-content/plugins folder. Each plugin has its own folder. The entry of the plugin should be the php file that has the same name as the folder. For example, if you name the folder sample you should create a file sample.php under that folder.

Plugin Meta data

In order for WordPress to understand your plugin, you have to put a comment inside the plugin php file (at the begining).

1
2
3
4
5
6
7
8
9
10
11
<php
/*
Plugin Name: Plugin Name
Description: Plugin Description
Version: 0.1
Author: SteakOverCooked
Author URI: https://steakovercooked.com
Plugin URI: https://steakovercooked.com
License: Free
Text Domain: sample-plugin
*/
<php
/*
Plugin Name: Plugin Name
Description: Plugin Description
Version: 0.1
Author: SteakOverCooked
Author URI: https://steakovercooked.com
Plugin URI: https://steakovercooked.com
License: Free
Text Domain: sample-plugin
*/

First Plugin

Based on this post, we can create a plugin that blocks most spam comments by checking HTTP_REFERER variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/*
Plugin Name: Check Comment Flood
Description: This Simple Plugin prevents most of the spam comments by checking the HTTP_REFERER variable. No complex configurations just simply activating the plugin will do the job. 
Version: 0.1
Author: SteakOverCooked
Author URI: https://steakovercooked.com
Plugin URI: 
License: Free
Text Domain: check-comment-flood 
*/
 
function check_referrer() {
    if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') {
        wp_die(__('Please do not access this file directly.'));
    }
}
add_action('check_comment_flood', 'check_referrer');
<?php
/*
Plugin Name: Check Comment Flood
Description: This Simple Plugin prevents most of the spam comments by checking the HTTP_REFERER variable. No complex configurations just simply activating the plugin will do the job. 
Version: 0.1
Author: SteakOverCooked
Author URI: https://steakovercooked.com
Plugin URI: 
License: Free
Text Domain: check-comment-flood 
*/

function check_referrer() {
	if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') {
		wp_die(__('Please do not access this file directly.'));
	}
}
add_action('check_comment_flood', 'check_referrer');

Save the file and refresh the plugin page, you will see this:

first-wordpress-plugin Wordpress Plugin Tutorial - How to Write First Plugin? plugin wordpress

first-wordpress-plugin

How is it better?

You can easily turn the plugin on and off. You don’t need to modify directly the function template (it something goes wrong, the wordpress will die). If you mess up the plugin, the plugin will be simply turned off. You can also upgrade the particular plugin (easy to maintain).

You don’t need to do anything if you upgrade the wordpress or the theme files.

github: check comment flood

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
491 words
Last Post: VPS Free Upgrade to 6 Cores - QuickhostUK
Next Post: How to Add Menu in the WordPress Top Admin Bar?

The Permanent URL is: WordPress Plugin Tutorial – How to Write First Plugin?

Leave a Reply