Browsing all articles tagged with php
Dec
17

How to create a Wordpress plugin

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 :)

Connect with me


What I'm Doing (via twitter)...

  • is applying for US visa. The website is not helping at all http://twitpic.com/16hvxp 5 days ago
  • @abumuaaz want to focus on my wireless project (yes, I get paid for that) and some development work. Ada job to pass ka? :) in reply to abumuaaz 6 days ago
  • is running 'sudo dd if=/dev/zero of=/dev/sda' on his laptop. Will it work, my computer forensic friends? 6 days ago
  • is on his first day of being jobless, hahaha. 1 week ago
  • finished reading Crowdsourcing: Why the Power of the Crowd Is Driving the Future of Business, in 32 hours. Next, Wikinomics. 1 week ago
  • is on his way to island hopping. Woohoo! 1 week ago
  • is leaving on a jetplane, don't know when he'll be back again... 1 week ago
  • missed his flight to KK. Will need to take the first flight tomorrow and pray he'll make it for his 9am meeting. 1 week ago
  • More updates...

Categories

Tags

bash blog book database debian embedded gutsy howto hsdpa html kde kubuntu linux lpi nature network Photoblog photography php pylons python recovery script security shell sqlalchemy ssh ubuntu vim wireless wordpress

Blogroll

Flickr Photostream
DSC 0336Do not pluck the tea leaves... Yeah, right.I am Super Faiq, and I have a D3!But we don't have much time to cover the whole area.The truth behind your cup of tea, muahahaha....It's my thing. I can do it eyes closed.We wander around the tea plantationTrying out Teletubbies modelingWe get bored easily, and within minutes we're already at a tea plantation.