How to build a WordPress plugin from scratch
WordPress have a great API for plugin developers. You can practically do anything with wordpress data, but first, you need to understand how the filters and actions work. You also need to know some minimal things that you have to add in any plugin to make it work. I will first start describing how the actions and filter works and how you can make wordpress to execute your code. Then I will describe the minimal code needed to add a menu item in the administration panel for your plugin.
1. What files a plugin should contain.
To start building a wordpress plugin, just create a new .php file where you will write your code. This file has to be uploaded in your wp-content/plugins/ folder. If you want to submit your plugin into WordPress directory, you will need also a readme.txt file. Create a directory and copy both files. The directory should be named the same as the .php file that you created. Let’s say that our plugin will be named “my-first-wp-plugin”, then you will have a directory called “my-first-wp-plugin”, with 2 files in it: “my-first-wp-plugin.php” and “readme.txt”. Since readme.txt file is optional I will skip it and go directly to the core plugin file.
The core plugin file should have a header, that will contain plugin name, description, version, author name and any other relevant information. Here is an example:
/*
Plugin Name: My First Wp Plugin
Plugin URI: http://myfirstwppluginsite.com
Description: This is my first wordpress plugin
Author: Lucian Apostol
Version: 1.0
Author URI: http://myhomepage.com
*/
This information is required for any plugin, and does not have anything to do with the rest of the code.
2. Understanding how WordPress Plugin API works.
WordPress API is based on actions and filters. An action is a function that you write in your plugin file. Then, you append your action to a hook. A hook is a place in WordPress core where your function will be executed. For example, you want to write a function, that will write your Google Analytics code at the end of every page of wordpress. You write the function that will display the code. You will assign this function to the hook called: “wp_footer” . When the rendering of a wordpress page will get to the call of wp_footer function, it will also execute your piece of code and the Google Analytics code will be displayed.
Here is the code:
add_action(‘wp_footer’, ‘my_first_plugin_function’);
function my_first_plugin_function() {
echo “Display Google Analytics Code here”;
}
Here is a list with all wordpress hooks for actions: http://codex.wordpress.org/Plugin_API/Action_Reference
A filter is also a function that you write, but it will take at least one parameter. The variable in the parameter will contain the content that you want to apply the filter for. To be more specific, I will give you an example. You want your plugin to add a line of promotional text at the end of every article, after the content is displayed. Now, you have to write a function that will get the content, and then append a line of text at the end of it.
Here is the code:
add_filter(‘the_content ‘, ‘add_a_promo_text’);
function add_a_promo_text($content) {
return $content . ‘<br />Write here your promotional text’;
}
You can just copy the above code, write it in your plugin file below the header, upload it to your wp-content/plugins directory, activate it trough your admin panel, and then refresh your page to see the changes.
Looking at the actions and filter hooks, you can think of places where you want your plugin to make changes. Now, you have the basics, and you can plan your future plugin. As you can see, the coding is very easy.
3. How to run code when your plugin is installed.
When you build a plugin, you may want to set some advanced options or even install some new tables in the database. It is reccomended to use the wordpress tables to write your data, but there are also cases when this is not possible.
register_activation_hook(__FILE__,’my_plugin_installation’);
function my_plugin_installation() {
//Write here the code that you want to be executed on plugin activation
}
3.1 How to create a new table in wordpress database
register_activation_hook(__FILE__,’my_plugin_installation’);
function my_plugin_installation() {
global $wpdb;
$table_name = $wpdb->prefix . “my_plugin_table”;
if($wpdb->get_var(“SHOW TABLES LIKE ‘$table_name'”) != $table_name) {
$sql = “CREATE TABLE ” . $table_name . ” (
id mediumint(9) NOT NULL AUTO_INCREMENT,
title text NOT NULL,
description text,
UNIQUE KEY id (id)
);”;
$wpdb->query($sql);
}
}
3.1 How to avoid creating a new table in wordpress database
Instead of creating a new table, you can use wordpress database structure to save your data.
If you want to save some general options or settings that you want to have available anywhere, you can use the functions add_option, update_option and get_option.
When you install the plugin, use the set_option function to set an option with a default value.
add_option( ‘option_name’, ‘option_default_value’);
To access tha saved option, use:
$option_value = get_option(‘option_name’);
If you want to save data specific to each post, then you can use the post meta table.
add_post_meta($post_id, $meta_key, $meta_value);
$meta_value = get_post_meta($post_id, $key, true);
When you are inside a function that is an action or a filter for a hook specific to a post, you can access the post ID trough the global variable $post. At the begining of your function declare global $post; , then you can access the ID trough: post->ID; .
4. How to create an administration page for your plugin.
To accomplish this, you will also need to write a function, that will display the content of the administration page.
add_action(‘admin_menu’, ‘my_plugin_menu’); function my_plugin_menu() { add_options_page(‘My Plugin Options’, ‘My Plugin’, ‘manage_options’, ‘page_url_slug’, ‘plugin_page_content_function’); }
function
plugin_page_content_function() { Write your code here }
Now you have the basics to write any WordPress plugin. Anything else you need you can find in the WordPress Codex. Don’t be afraind to search.
Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the item, I will receive an affiliate commission
Leave a Reply