A while ago I wrote a small tutorial on how a WordPress theme is created, now I would like to share some “secrets” of WordPress plugin programming, so here we go
.
So what kind of plugin are we going to create? Let’s do a plugin that gets some WordPress data from the database and puts it on a static page. shall we.
The first thing we need to do is create a plugin file with the correct information in the header so that WordPress can identify it as a plugin. We will call this plugin wp-plugin. The normal thing to do is give the plugin file the same name as the plugin itself, hence the name for our file is wp-plugin.php
. We put this file in the wp-content/plugins directory of our WordPress installation .
It’s all in the head
The header of wp-plugin.php contains information about plugin name, author name and author email, web site an so. The most important is the plugin name
.
wp-plugin.php
When the user visits the WordPress admin->plugins WordPress scans the plugins directory for plugin files so if the header is correct the file is added to the interface and can be activated or deactivated.
Lets take a look at the code. As you can see we have specified some meta information of the plugin, the code is self explanatory and not hard to understand. This is what WordPress uses to mark the file as a plugin as long as it’s located in the correct folder.
Hooks and filters
For the plugin to interact with WordPress it need some API to interact with so WordPress has it’s own API for plugins.
Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress. There are two kinds of hooks:
Hooks that take Actions. Actions are functions that specify additional events that can occur triggered by core WordPress events. Action Hooks, then, enable the addition and removal of functions whose execution will be triggered by those WordPress actions. These hooks enable plugins to execute functions each time the specified event occurs.
Hooks that take Filters. Filters are functions that modify WordPress content stored in your database
. Filter Hooks allow you to add or remove Filters to various types of WordPress content (usually text). These enable plugins to modify content or text on-the-fly.
From a coding perspective, actions and filters are nearly identical. The add_action function is nothing more than an alias for the add_filter function. In practice, they are useful for different things.
You can sometimes accomplish the same goal with either an action or a filter. For example, adding an action to publish_post or a filter to the_content. Choose based on whether you want the change to be made once or to be made every time the content is accessed.
So lets write some code
|", $url, $content);
}
/**
* This will be added to the header of WordPress trough the
* add_filter below.
*/
function my_header()
{
}
/**
* Install filter and hooks.
* WordPress sends the content from the
* database to the my_function above
3. Patients may change medication regimens, either4. General medical and psychosocial reassessment viagra kaufen.
properly assessed and advised patients. buy viagra online masturbation is a sensitive issue that is often.
In the USA, the Massachusetts Male Aging Study, reported cialis without prescription Studies have been performed in accordance with GCP standards..
.
*/
add_filter('the_content', 'my_content');
/**
* This will be added to the header of the WordPress
* before it is displayed, perfect for adding your own css or scripts.
*/
add_action('wp_head','my_header')
Let’s take a look at the code above. At the end we define our hooks and filters that interact with the API. The function my_content($content) is where it all happens. This function takes the content generated from the database such as the content of a page or a post. This is done via the API and WordPress is responsible for the content that is send to the function. In this function we retrive some trivial data from the database and puts it in a variable.
Now the difficult part comes. We have to replace our tag in the static page with our data from the database, and that is done with preg_replace, we simply replace our whith the content in our variable. Last we returns the content back to WordPress so it can be displayed.
For this to work we need to create a static page and somewhere we put our tag to replace.
Summary
- Create a file
- Add the header
- Add hooks and filter
- Create the functions
- Replace data and return the content
bedava mp3 indir
19 years ago
Good Sharing Thanks for sharing 😉
sikomat
19 years ago
thanks for the inspiration.
blogw?ºrdig » Anleitungen f?ºr Wordpress
19 years ago
[…] Bei Fredrik Fahlstad gibts eine Anleitung zum Erstellen eines WP-Templates und eine zum Erstellen eines Plugins. Tag: WordPress […]
Benjamin
19 years ago
This is actually somethign i was looking into recently. Thanks for sharing.
Gvadelup
19 years ago
catalogood
Heidzir
18 years ago
dude,
you make my day. your article light my path creating plugin…
lil wyte
18 years ago
Hello!
I installed WordPress on my server, and set up a site. It’s a relly nice piece of software. However, I am missing a piece of functionality, and
couldn’t find a suitable answer for it. Mayou you know a plugin I could use?I need to automatically inserts ads in my posts.And not AdSense ads, but rather HTML snippets defined by me (affiliate links, etc). Let’s say I would insert a tag in my post:
— ad here —
and the plugin would automatically replace it with some HTML code, when the post is displayed on my site.I really don’t need any fancy configuration options etc, just the basic replace functionality.
Do you know a plugin that can handle this?
Fredrik Fahlstad
18 years ago
lil wyte: No, I’m sorry.
Silver
18 years ago
Hi,
maybe you can help me
i have a plugin wich is worked with the older versions of WP.
But now i always get the following:
Warning: Cannot modify header information – headers already sent by (output started at wp-content/plugins/blogring.hu_pinger.php:68) in wp-includes/pluggable-functions.php on line 272
This will come up when i try to catch the publish_post hook:
add_action(‘publish_post’, ‘my_function’);
if my_function is an empty func i got the same. What’s the problem with add_action?
Antibush
18 years ago
Bush and the Republicans were not protecting us on 9-11, and we aren’t a lot safer now. We may be more afraid due to george bush, but are we safer? Being fearful does not necessarily make one safer. Fear can cause people to hide and cower. What do you think? Why has bush turned our country from a country of hope and prosperity to a country of belligerence and fear.
If ever there was ever a time in our nation’s history that called for a change, this is it!
The more people that the government puts in jails, the safer we are told to think we are. The real terrorists are wherever they are, but they aren’t living in a country with bars on the windows. We are.
Fahlstad Design Secrets of a WordPress plugin | fix my credit
15 years ago
[…] Fahlstad Design Secrets of a WordPress plugin Posted by root 57 minutes ago (http://www.fahlstad.se) Let do a plugin that gets some wordpress data from the database and puts it on a static actions are functions that specify additional events that can occur triggered by core wordpress events we are you must be logged in to post a comment copyright fredrik Discuss | Bury | News | Fahlstad Design Secrets of a WordPress plugin […]