How to Create a Simple Wordpress Plugin
Posted on 10 June, 2009 at 19:49 PM
There are already 14 Comments
About the Show
In this screencast, Marc runs over his code for a simple "Twitter Directory" plugin for Wordpress, explaining certain details and sharing tips along the way.
The following issues are attended to in this post:
- Adding an Activation Hook to your plugin.
- Creating new tables in your Wordpress database using SQL.
- Running a custom database Query.
- How to get wp_redirect() to work without throwing the "Cannot modify header information" error.
- Correct usage of the add_menu() function.
Download the OCMX Twitter List plugin HERE
Useful links related to this post:
Related Shows
- (41) EP 168 - A New Beginning
- (32) Wireframing over the shoulder
- (24) Keeping your elements alive using jQuery.live()
- (11) Example of creating hype before a product launch
- (41) How to Add Detail to a Design Element
User Comments
Hugh Collier 11 Jun, 2009 00:30:32 AM
This is way over my head at the moment (I'm literally just starting out with PHP) but I'm sure it'll come in handy once I get a bit better.
I'm on lesson 3 or 4 of themeforest's dive into PHP video series which I'm finding absolutely brilliant. I'd totally recommend it to any beginners who find this tut a bit advanced/involved. Link is here:
www.multiurl.com/ga/Diving_Into_PHP
Sam 11 Jun, 2009 00:38:37 AM
Great tutorial! Next step is so you only have to enter the twitter name and it'll look up their actual name for you :-)
One criticism though, I know this is in the admin pages, but you should never do something like "SELECT * FROM table WHERE id=" . $_GET['id'] as you're just opening up your query for a SQL injection attack. Make sure you validate your variables before passing them to SQL (ie. in this case, id must be a number).
Marc Perel 11 Jun, 2009 00:48:16 AM
@Sam - Very good point mate, thanks for pointing that out.
My bad - *Runs and hides in a corner*
Dave Kirk 11 Jun, 2009 02:23:01 AM
Nice tutorial, I have a couple of comments, both relating to specifity,
1 - When declaring you $plugin_prefix, you should specify it is a global variable, otherwise it won't be available in certain situations, ie:
global $plugin_prefix; $plugin_prefix = "ocmx_";
2 - The same also applies to the table name, in your 1st file ocmx-twitter-list.php you should declare it as global, then in the setup file, you can simply reference the that variable, rather than re-declare the variable
global $wpdb; global $twitter_table; $twitter_table = $wpdb->prefix . 'table_name';
This will mean that both your variables will always be available, otherwise they may not be available to you during activation, etc
Dave Kirk 11 Jun, 2009 02:26:20 AM
With reference to my previous comment, see Register Activation Hook Documentation and read the section on Variable Scope
Dave Kirk 11 Jun, 2009 02:27:21 AM
Link codex.wordpress.org/Function_Reference/register_activation_hook
Marc Perel 11 Jun, 2009 10:41:54 AM
@Dave is 100% about setting global variables for easier access, thanks Dave.


Rob MacKay 10 Jun, 2009 20:05:50 PM
I've been deep in learning PHP all day so this is very well timed :) thanks dude its a great overview I will be watching it a good few times.