Now let's see how to create a WordPress plugin. Why this post? Often WP users active a plugin and use only the 5% of its features. Is not a good choise. There are people who boasts of using 50 or more plugin. IMHO they just wrong CMS :)
In any case plugins help us to make several chenges in our blogging platform without change che core code and the template used, just activating / disactivating a file by the control panel.
Now, to explane how a WP plugin can be create, we'll see the needed operation to produce a simple plugin showing a fixed text before any post. Really unusefully plugin, but with clear didactic purpose.
How To Begin
First of all, it's very important read the documentation released by the WP team, to know as well as possible all the functions availables given by the wp core.
After this, you have to know that wordpress use a single database field to activate a plugin. The informations concerning the actives plugins are stored in the wp_options table (where wp_ is just the table prefix) in correspondants of the "option_name = 'active_plugins'" value.
All plugins file are stored in /wp-content/plugins/.
Begin Writing
With our favorite text editor create a new document called myplugin.php. After the php open tag, it must be inserted some comment line that will be used by identify the plugin name, author, version and description.
<?php
/*
Plugin Name: Just a test plugin
Plugin URI: http://www.davidonzo.com/
Description: Just a simple plugin to show how wordpress plugins work and can be create.
Author: davidonzo
Version: 0.1
Author URI: http://www.davidonzo.com/
*/
- Plugin Name: choose a funny name for your plugin.
- Plugin URI: have you publishe a post or an entire web site for the plugin?
- Description: write a short description of the plugin. In one line. Break line will erase the content!
- Author: your name.
- Version: the versione number of the plugin.
- Author URI: your link.
These informations will be showed in the management plugins page of wordpress's admin control panel.
The necessaries functions
What do you need to make the plugin? We said we want to create a plugin tha add a fixed text, choosen by the user, before any post. So, we need an option where writing the text and another one where we'll use as a boolean value to make the visualization active or not.
To make it easy, we choose to save both options in just one record. Using a text separator between the string to provide a simply explode() string that extract the needed value. In this case we will use %%%.
Now, we need to know that the plugins options are stored in the same table used by WP for the others system options: wp_options. We che easly create new options just using a function available in the WP core: add_option($nameoption, $value). The option's $value will be stored in a longtext field.
During the plugin installation a new option will be created, assigning it the default values. Let's see who to do it!
function myplugin_install(){
if(get_option('opt_myplugin' == '') || !get_option('opt_myplugin')){
add_option('opt_myplugin', '0%%%Write some text');
}
}
if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
myplugin_install();
}
On plugin activeting, if the option named opt_myplugin doesn't exist or is empty, the option will be added to the database with the default values.
Note that this function will be loaded for any activating action, but the default value and the option creation will be execute just at the first activation. So, deactiving the plugin, the latest values inserted will be saved and used after the new activation.
Now we need a pege inside the WP control panel, to manage the options. First of all, we add a link to the main menu of the settings page. Of couse we'll use a wp core function :)
function myplugin_config_page(){
if (function_exists('add_options_page')){
add_options_page('MyPlugin Menu Title',
'MyPlugin Option',
8,
basename(__FILE__),
'myplugin_config'
);
}
}
To work correctly, add_option_page function need some variable.
- The title of the destination page.
- The anchor link for the title menu.
- The access level required to manage the settings.
- The file must be loaded.
- The callback function contained in the loaded file, that create the page.
Well, let's see the callback function.
function myplugin_config(){
$active = (myplugin_option('active') == 1) ? 'checked' : '';
$deactive = (myplugin_option('active') == 0) ? 'checked' : '';
print('<div class="wrap">
<h2>MyPlugin Options Tab</h2>
<form id="ak_sharethis" name="myplugin_cnf" action="'.get_bloginfo('wpurl').'/wp-admin/index.php" method="post">
<p>By this page it\'s possibile configure the plugin. Setup the following options to start testing how a wordpress plugin works!</p>
<fieldset class="options">
<p>Show some content before any post?</p>
<ul>
<li>
<input type="radio" name="c_before_post" value="1" id="c_before_post_yes" '.$active.' />
<label for="c_before_post_yes">Yes</label>
</li>
<li>
<input type="radio" name="c_before_post" value="0" id="c_before_post_no" '.$deactive.' />
<label for="c_before_post_no">No</label>
</li>
</ul>
<p>Type the contet text to show:</p>
<input type="text" name="c_text_myplugin" value="'.myplugin_option('text').'" />
</fieldset>
<p class="submit">
<input type="submit" name="myplugin_submit_button" value="Update Settings" />
</p>
</form>
</div>');
}
Just HTML containing an usefull form to manage the main plugin's options. Nothing more, nothing less :)
Ok, now we have a form to manage the option, but, how can we update them? Just writing another function.
function update_myplugin_settings(){
if($_POST['myplugin_submit_button'] == 'Update Settings'){
$newsetting = $_POST['c_before_post'] . '%%%' . $_POST['c_text_myplugin'];
update_option('opt_myplugin',$newsetting);
header('Location: '.get_bloginfo('wpurl').'/wp-admin/options-general.php?page=myplugin.php&updated=true');
}
}
Using update_option (another WP core function) we can easly update any option just declaring as first argument the option to update, and as second argument, the value to assign.
To execute it, just use add_action.
add_action('init', 'update_myplugin_settings', 9999);
Using the tag init and a height priority (9999) this action will be performed any time the plugin file is requested. But read the callback function as well: the real option update process will run just when it receive a particular POST data.
Now we have a page who manage the options, we can update them, we can activate the plugin as well, but, where is the function who chage my wordpress front-end?
function add_myplugin_text_to_content($content){
if(myplugin_option('active') == 1){
$content = '<p>'.myplugin_option('text').'</p>' . $content;
}
return $content;
}
add_action('the_content', 'add_myplugin_text_to_content');
Just a simple function, activated when the_content() function run, that replace $content variable.
Do you want to download this example wp plugin? Just click here :)








