developing wordpress plugins

24
Developing WordPress Plugins Markku Seguerra http:// rebelpixel.com

Upload: rebelpixel

Post on 08-May-2015

21.647 views

Category:

Technology


0 download

DESCRIPTION

A talk on developing WordPress plugins, basically an introduction on how to extend the most popular blogging app today. Used for WordCamp Philippines 2008.

TRANSCRIPT

Page 1: Developing WordPress Plugins

Developing WordPress Plugins

Markku Seguerrahttp://rebelpixel.com

Page 2: Developing WordPress Plugins

What is a WordPress plugin?

Little applications used to enhance functionality or add specific functions tailored to a site's specific needs.

Page 3: Developing WordPress Plugins

Some plugins:

- Akismet- WordPress Database Backup- WordPress.com Stats- wp-recent-links- Comment Hilite

Page 4: Developing WordPress Plugins

What do you need to make a plugin?

- a problem to solve- some PHP knowledge- some spare time- a test server with your test WordPress (XAMPP is good.)

Page 5: Developing WordPress Plugins

Structure: Things to remember

- A unique descriptive name- Naming: myplugin.php or /myplugin/ folder- Readme.txt format forwordpress.org/extend/plugins- Plugin home page- File headers (very important!)

Page 6: Developing WordPress Plugins

Headers<?php/*Plugin Name: Name Of The PluginPlugin URI: http://mypage.com/myplugin/Description: What does it do?Version: 1.0Author: Name Of The Plugin AuthorAuthor URI: http://mypage.com/*/?>

Page 7: Developing WordPress Plugins

Include your license details!

The GPL (and compatible licenses) is commonly used for plugins.

Page 8: Developing WordPress Plugins

Plugin ProgrammingBefore WP 1.2, customizations required

altering the core files, causing conflicts among hacks (remember my-hacks.php?) and making upgrades very tedious.

The solution?

Page 9: Developing WordPress Plugins

Plugin API

- Enabled "hooks"- Extend functionality withoutediting the core code- Two categories:Actions and Filters

Page 10: Developing WordPress Plugins

Actions

Specific points in the WordPress code that can be used to trigger plugin-specified events and functions.

add_action( 'hook_name', 'your_function_name', [priority], [accepted_args] );

Page 11: Developing WordPress Plugins

Sample action: “wp_login”

function notify_on_login() { // your code here // email to admin, etc... } add_action('wp_login', 'notify_on_login');

Page 12: Developing WordPress Plugins

Filters

Functions that modify text, lists andvarious types of information that areused and produced by WordPress.

add_filter('hook_name', 'your_filter_function', [priority], [accepted_args]);

Page 13: Developing WordPress Plugins

Sample filter: “the_content”

function add_rss_invite(){ // output to screen the link to RSS feed // if 1st time visitor}

add_filter('the_content', 'add_rss_invite');

Page 14: Developing WordPress Plugins

Template Tags

Plugins can also be used to generatespecial template tags that displaycustom content.- Recent comments- Top posts- Ad display

Page 15: Developing WordPress Plugins

Storing Plugin Data

- For large amount of data, create your own database table.

- For fairly small and/or static data, use built-in WP "Options" capability.

add_option($name, $value, $deprecated, $autoload);get_option($option);update_option($option_name, $newvalue);

Page 16: Developing WordPress Plugins

Administration Menus & Pages

- There are specific functions to add pages and menu items.

add_menu_page(page_title, menu_title, access_level/capability, file, [function]);

add_submenu_page(); add_options_page();add_management_page();add_theme_page();

Page 17: Developing WordPress Plugins

Other things to consider- Internationalization- WordPress Coding Standards & inline documentation- Function prefixes to avoid name collision- When creating tables, use $wpdb->prefix- Minimize database writes.- Write secure code! (Use nonces, sanitize, etc.)- Be aware of user roles and capabilities.- Stick to the WordPress API to avoid problems!

Page 18: Developing WordPress Plugins

Sample plugin: Strip!<?php/*Plugin Name: Strip!Plugin URI: http://rebelpixel.com/Description: Removes hyperlink tags from a given comment.Version: 0.1Author: Markku SeguerraAuthor URI: http://rebelpixel.com/projects/strip/*/

Page 19: Developing WordPress Plugins

/* Copyright 2008 Markku Seguerra (email : [email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*/

Page 20: Developing WordPress Plugins

add_filter('comment_text', 'strip_comments_add_admin');function strip_comments_add_admin($text){ // add the strip button to the comment}

add_filter('comment_text', 'strip_comments_add');add_filter('get_comment_author_link', 'strip_comments_add');function strip_comments_add($text){ // mark the comment as stripped and displays // it without links}

Page 21: Developing WordPress Plugins

add_action('wp_ajax_strip', 'do_strip');function do_strip(){ // function to mark comment as stripped // triggered via ajax}

add_action('wp_ajax_unstrip', 'do_unstrip');function do_unstrip(){ // function to mark comment as stripped}

function strip_selected_tags($text, $tags = array()){ // our filter function that removes links}

Page 22: Developing WordPress Plugins

add_action('admin_head', 'strip_comments_head');function strip_comments_head(){ // show the necessary css and ajax // functions used in the admin interface}

Page 23: Developing WordPress Plugins

<script type="text/javascript">//<![CDATA[function strip_now(cid) { jQuery.post("<?php echo get_option('siteurl'); ?>/wp-admin/admin-ajax.php", {action:"strip", "c":cid, "cookie": encodeURIComponent(document.cookie)}, function(str) { pn = '#p-' + cid; jQuery(pn).html(str); });}

function unstrip_now(cid) { jQuery.post("<?php echo get_option('siteurl'); ?>/wp-admin/admin-ajax.php", {action:"unstrip", "c":cid, "cookie": encodeURIComponent(document.cookie)}, function(str) { pn = '#p-' + cid; jQuery(pn).html(str); });}

//]]></script>

Page 24: Developing WordPress Plugins

Thank you!

Markku Seguerrahttp://rebelpixel.com