developing plugins for wordpress

18
DEVELOPING PLUGINS FOR WORDPRESS Lester Chan | http://lesterchan.net

Upload: lester-chan

Post on 19-May-2015

2.747 views

Category:

Software


3 download

DESCRIPTION

My presentation on Developing Plugins for WordPress during Singapore PHP User Group Meetup in June 2008

TRANSCRIPT

Page 1: Developing Plugins For WordPress

DEVELOPING PLUGINS FOR WORDPRESS

Lester Chan | http://lesterchan.net

Page 2: Developing Plugins For WordPress

Agenda

Introduction Developing Plugin

WP API: Filters/Hooks Plugin Path Plugin Details Inserting CSS/Javascript Modifying Content Translating Plugin Text To Other Languages Shortcode API Activating The Plugin

Page 3: Developing Plugins For WordPress

Introduction

Name Lester Chan

Website http://lesterchan.net

Experience Developing WordPress Plugins the day WP

is born Developed 16 WP plugins to date

WP-Polls, WP-PostRatings, WP-PostViews, WP-Print, WP-Email, WP-Useronline, etc

Page 4: Developing Plugins For WordPress

WP:API Filters/Hooks

Filters Filters allows your plugin to modify data that

passes through WordPress at specific locations Eg: post title, post date, post content add_filter(‘location’, ‘plugin_function_name’);

Hooks Hooks allows you to execute your plugin

function at specific location when WordPress is being executed Eg: Init, header, footer add_action(‘location’, ‘plugin_function_name’);

Page 5: Developing Plugins For WordPress

Plugin Path

All WordPress plugins are placed in: /wp-content/plugins/

Example: Name: WP-Demo Nice Name: wp-demo

Placing your plugin in: /wp-content/plugins/wp-demo.php /wp-content/plugins/wp-demo/wp-demo.php

Page 6: Developing Plugins For WordPress

Plugin Path

My naming convention style: /wp-content/plugins/wp-demo/

wp-demo.php demo-css.css demo-js.js demo-js-packed.js demo-options.php demo-uninstall.php

Page 7: Developing Plugins For WordPress

Plugin Details

<?php/*Plugin Name: WP-DemoPlugin URI: http://lesterchan.net/wp-demo/Description: This is a demo pluginVersion: 1.00Author: Lester ChanAuthor URI: http://lesterchan.net*/?>

Page 8: Developing Plugins For WordPress

Inserting CSS/Javascript

Inserting CSS: function print_css() {

echo '<link rel="stylesheet" href="'.get_option('siteurl').'/wp-content/plugins/wp- demo/demo-css.css" type="text/css" media="screen" />‘;}

Use Hook: wp_head add_action('wp_head', ‘print_css');

Page 9: Developing Plugins For WordPress

Inserting CSS/Javascript

Inserting Javascript: Use Hook: wp_head Use 2 WP Functions:

wp_register_script() wp_print_scripts()

wp_register_script('wp-demo', '/wp-content/plugins/wp-demo/demo-js-packed.js', false, '1.00');

wp_print_scripts(array('jquery', 'wp-demo'));

Page 10: Developing Plugins For WordPress

Inserting CSS/Javascript

Advantages of using register/print script: Print only once

Good practice to compress/obfuscate Javascript Smaller File Size Load Faster http://dean.edwards.name/packer/

WordPress 2.6 wp_register_style() wp_print_styles()

Page 11: Developing Plugins For WordPress

Modifying Content

function demo_content($content) {return ‘Start of post’.$content.’End

of post’;}

Use Filter: the_content add_filter('the_content', 'demo_content');

Page 12: Developing Plugins For WordPress

Translating Plugin

Loading The Translation: function demo_textdomain() {

load_plugin_textdomain('wp-demo', 'wp- content/plugins/wp-demo');}

Use Hook: init add_action('init', 'demo_textdomain');

Page 13: Developing Plugins For WordPress

Translating Plugin

Printing the text Instead of

<?php echo ‘Testing’; ?>

Use <?php _e(‘Testing’, ‘wp-demo’); ?>

Return the text <?php __(‘Testing’, ‘wp-demo’); ?>

Page 14: Developing Plugins For WordPress

Translating Plugin

Poedit http://www.poedit.net/

Tutorial http://lc.sg/q

Page 15: Developing Plugins For WordPress

ShortCode API

What is ShortCode API? Shortcode API, a simple set of functions for

creating macro codes for use in post content.

Example: [demo id=“1”] Is able to retrieve a demo record of ID 1 and

embed the results into the post content

Page 16: Developing Plugins For WordPress

ShortCode API

Implementation [demo id=“1”] add_shortcode('demo', 'demo_shortcode');

function demo_shortcode($atts) {extract(shortcode_atts(array('id' => 0),

$atts)); return get_demo_by_id($id);}

Get Demo By ID Function function get_demo_by_id($id) {

// Perform Database Queryreturn '<p>ID:'. $id.'</p>‘;

}

Page 17: Developing Plugins For WordPress

Activating The Plugin

add_action('activate_wp-demo/wp-demo.php', 'create_demo_table');

global $wpdb; $wpdb->demo = $wpdb->prefix.'demo'; function create_demo_table() {

global $wpdb;…

}

Page 18: Developing Plugins For WordPress

THANK YOU

ANY QUESTIONS