/home/shakir

How to create a Wordpress plugin

Posted on 17 Dec, 2007, categorized under Information Insemination



I’m into writing a plugin for Wordpress, and so let me just share here how easy it is to actually create one.

For an example, we’re going to write a plugin that will insert a Google Adsense to our blog post.

From our Wordpress installation directory, create a file named google_adds.php in the wp-contents/plugins/ directory and add these lines; 

<?php
/*
Plugin Name: Google Adsense
Version: 0.1
Plugin URI: www.mohdshakir.net
Description: This plugin will display Google Adsense in every post
Author: Mohd Shakir bin Zakaria
Author URI: www.mohdshakir.net
*/
?>

To see what it does, login to your Wordpress admin panel and go to the Plugins section.

As what can be seen from the screenshot, the information of the plugins there were taken from the comments on our plugin code. We can now activate the plugin, but for now our plugin doesn’t really do anything useful.

Let’s go back to our PHP code, and add some lines to be as the following

<?php
/*
Plugin Name: Google Adsense
Version: 0.1
Plugin URI: http://www.mohdshakir.net
Description: This plugin will display Google Adsense in every post
Author: Mohd Shakir bin Zakaria
Author URI: http://www.mohdshakir.net
*/

function adsense($text){

        $adsense_code = <<<EOT
# Add your own Adsense code here
EOT;

        return $adsense_code . "<br \>"  .$text;
}

/*
  Add filter is a built in Wordpress function, and the_content
  is a Wordpress variable of a single post content.
*/
add_filter('the_content', 'adsense');

?>

Let’s see if it works

That’s it, our first fully functioning Google Adsense plugin :)





Looking for something else? Search Google.

Comment Form


  • si perda: wah.. boleh kasi need for speed test power skali... boleh layan network race ni...
  • Faiz: wah....harganya berapa?
  • shakir: Hmm, I have no lab and that actually was my living room :D Sure I'll move them to one of my available rooms at home and make it a lab, but that'll be
  • peja: whoaaa..at least u have ur own lab...look interesting!
  • Qing Ru: He looks really cute! Fatherhood must have been quite rewarding :)
  • shakir: Chris: Thanks, I've updated the post.
  • shakir: tak cukup banyak guinea pig yang dah try and comment on the wimax service, and so I'll just wait :D
  • shakir: Vadim: Yup, but I prefer to just install dosbox and play the game rather than re-installing my system with 32 bit Linux :D
  • Vadim: 2shakir: wine doesn't support 64-bit linux. You have to use 32-bit linux if you want to use wine.
  • kevler: yerp ...dalam 2 hari nih jer ..aku dah kesan 2 BMW X6 , kat puchong dan jalan ampang ..giler laa lawa



Disclaimer

The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my own personal opinion. Inappropriate comments will be deleted at the authors discretion. All code samples (if any, ever) are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Personal (Blogs) - TOP.ORG