Tutorial Custom Plugin for WordPress

My first post about WordPress “customization”. What I mean with customization is not a real customization. It is just a sample “how to create a new plugin”. Don’t do like just like what I did to my blog before, so we can add our own stuff safely without smashing the original code. Btw, also don’t ask me question why I wrote this post using English instead of using Bahasa hehehe.

First, open the text editor (prefer to use code editor such Notepad++ for Microsoft or Smultron for Mac *my most favourite*). Then I would give an example how to add and put our own customization css file into header tag. So it would be seen like this :

	<!--wp origin header [start]-->
	blah blah blah
	<!--wp origin header [end]-->
	<!--our amazing script [start]-->
	<link rel="stylesheet" type="text/css" href="ourcssfile" />
	<!--our amazing script [end]-->

Will not be properly useful if I put that script into each child theme’s header. So how do I put that css link tag automatically? Once again the answer is using plugin. Ok, create a new PHP file and hook an action for our css link tag like below.

 * @package custom plugin name
 * @version 1.0

Plugin Name: custom plugin name
Plugin URI: our url
Description: our description
Author: our name or alias
Version: 1.0
Author URI: our url
add_action( 'wp_head', 'put_css', 1);
function put_css(){
	echo '<link rel="stylesheet" type="text/css" href="ourcssfile" />';

See? Just upload the PHP file into wp-content/plugins/ourpluginfolder/ then activate via wp-admin. Finally, what we have done? Only need to create a function named put_css() and hook that function into an action called wp_head by using add_action. What is wp_head function? That’s an action function which used for add elements into html header. Guidance? Refer to WordPress.org’s docs.

Without compilation (of course this is not Java LOL), voila! Don’t forget to activate our brand new plugin, so that link tag is supposed to be appear automatically after the page is loaded (if we are not missed a thing). No need to put our script manually for each theme’s header, that’s hurt.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s