WordPress allows developers to add their own code during the execution of a request by providing various hooks. These hooks come in the form of actions and filters:
Actions: WordPress invokes actions at certain points during the execution request and when certain events occur.
Filters: WordPress uses filters to modify text before adding it to the database and before displaying it on-screen.
The number of actions and filters is quite large, so we can’t get into them all here, but let’s at least take a look at how they are used.
Here’s an example of how to use the admin_print_styles action, which allows you to add your own stylesheets to the WordPress admin pages:
1 2 3 4 5 6 7 8 9 |
add_action('admin_print_styles', 'myplugin_admin_print_styles'); function myplugin_admin_print_styles() { $handle = 'myplugin-css'; $src = MYPLUGIN_PLUGIN_URL . '/styles.css'; wp_register_style($handle, $src); wp_enqueue_style($handle); } |
And here’s how you would use the the_content filter to add a “Follow me on Twitter!” link to the bottom of every post:
1 2 3 4 5 6 7 8 9 |
add_filter('the_content', 'myplugin_the_content'); function myplugin_the_content($content) { $output = $content; $output .= '<p>'; $output .= '<a href="http://twitter.com/username">Follow me on Twitter!</a>'; $output .= '</p>'; return $output; } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Plugins, wordpress