Sunteți pe pagina 1din 75

Beginning WordPress

Plugin Development
Aizat Faiz aizat.faiz@gmail.com

Download and view at


http://blog.aizatto.com/?p=3729

Creative Commons

http://www.flickr.com/photos/kwl/4435490471/

By Attribution

http://www.flickr.com/photos/balakov/4571468943/

Aizat Faiz

aizat.faiz@gmail.com

http://www.flickr.com/photos/byte/3397174843/

Your Friends / Resources


http://www.flickr.com/photos/balakov/4571468943/

WordPress Codex
http://codex.wordpress.org

WordPress Plugin Directory

Google

http://www.google.com.my

What is a Hook?

http://www.flickr.com/photos/kwl/4247555680/

WordPress is the Base

http://www.flickr.com/photos/kwl/4247555680/

Plugins are the Blocks

http://www.flickr.com/photos/sixteen-miles/3757674953/

Hooks are the Pegs

http://www.flickr.com/photos/linuxmatt/92802487/

2 Kinds of WordPress Hooks

Actions
Filters

Actions

Actions

Actions are the hooks that the WordPress core


launches at specific points during execution,
or when specific events occur. Your plugin can
specify that one or more of its PHP functions
are executed at these points, using the Action
API.
http://codex.wordpress.org/Plugin_API#Actions
http://codex.wordpress.org/Plugin_API/Action_Reference

Actions

Actions = Do Something

Filters

Filters
Filters are

functions that WordPress passes


data through, at certain points in execution,
just before taking some action with the data
(such as adding it to the database or sending
it to the browser screen). Filters sit between
the database and the browser (when
WordPress is generating pages), and between
the browser and the database (when
WordPress is adding new posts and
comments to the database); most input and
output in WordPress passes through at least
one filter. WordPress does some filtering by
http://codex.wordpress.org/Plugin_API#Filters
default, and your plugin
can add its own
http://codex.wordpress.org/Plugin_API/Filter_Reference
filtering

Filters

Filters = Transform

Actions and Filters

Actions = Do Something
Filters = Transform

1133

1133
hooks

* as of WordPress v2.9

Problem:
Finding the Right Hook for the Right Job

Hard to Recommend

Look at the resources I gave you

WordPress Codex

WordPress Plugin Directory

Google

Action and Filter Reference

http://codex.wordpress.org/Plugin_API/Action_Reference

http://codex.wordpress.org/Plugin_API/Filter_Reference

Writing Your First Plugin

WordPress Directory Structure

All Plugins are stored in:


/wp-content/plugins/

Plugin Directory Structure

Your Plugin:

/wp-content/plugins/my-plugin

Inside my-plugin

readme.txt
screenshot-1.png
my-plugin.php

Always put it in a directory!

Uses 'dashes' and not 'underscores'


http://codex.wordpress.org/Writing_a_Plugin#Names.2C_Files.2C_and_Locations

readme.txt

Useful only for publishing to


WordPress Plugin Directory
Information about your plugin:
Description, Installation, Changelog, Donation
Links, Tags, etc...
http://codex.wordpress.org/Writing_a_Plugin#Readme_File
http://wordpress.org/extend/plugins/about/readme.txt

screenshot-1.png

Useful only for publishing to


WordPress Plugin Directory

my-plugin.php
Your Plugin Code

4 parts to a plugin

Plugin Header

Hooks

PHP Code

Template Code

File Structure

Plugin Headers

http://codex.wordpress.org/Writing_a_Plugin#Standard_Plugin_Information

Plugin Headers

Always on top, no choice

Fill in with your own details

http://codex.wordpress.org/Writing_a_Plugin#Standard_Plugin_Information

Hooks (Filters)

Hooks (Filters)

After plugin headers (my preferance)

Makes it easier to find

PHP Code

Plugin 1
Figure out what you want to do.

I want to convert all instances of


WordPress to WORDPRESS
in a post's content.

the_content (filter)

http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

add_filter

http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

add_filter

Hook Name
Callback
http://codex.wordpress.org/Plugin_API#Hook_to_WordPress

Hook Callback

Determines what PHP function to call

Callback can be either:

String; or

Array of 2 strings (my preference)

Hook Callback

String

Calls a function

Array of 2 strings

Calls a static function in a class

They do the same thing

I Prefer Array Callbacks

Allows me to segment my code

Lower chances of name conflicts

Easily tell which function belongs to which


hook

Filters are Transformations

filters have to return a transformation

Filter return Values

A filters return value, is the result of the


transformation

return
transformation
http://php.net/manual/en/function.preg-replace.php

Plugin 2
Figure out what you want to do.

I want to BOLD all instances of


WORDPRESS
in a post's content.

Hook Priority

http://codex.wordpress.org/Plugin_API#Hook_to_WordPress

Priority

Hook Name
Callback

Priority

(optional)

http://codex.wordpress.org/Plugin_API#Hook_to_WordPress

WordPress2WORDPRESS#the_content

Which one goes first?


ABolderWordPress#the_content

Default Priority

10
smaller numbers = higher priority
larger numbers = lower priority
http://codex.wordpress.org/Plugin_API#Hook_to_WordPress

Therefore

Order of execution:
(10) WordPress2WORDPRESS#the_content
(20) ABolderWordPress#the_content

Plugin 3
Figure out what you want to do.
I want to add a class,
to represent a post
that has more than 10 comments

accepted_args

http://codex.wordpress.org/Plugin_API#Hook_to_WordPress

accepted_args

Hook Name
Callback
Priority

accepted args (optional)


http://codex.wordpress.org/Plugin_API#Hook_to_WordPress

accepted_args

Number of arguments for the filter

Plugin 4
Figure out what you want to do.
I want to add a
custom stylesheet

init (action)

http://codex.wordpress.org/Plugin_API#Hook_to_WordPress

wp_enqueue_style

WordPress function
Style ID
Style URL

http://codex.wordpress.org/Function_Reference/wp_enqueue_style

Actions Do Something

Actions do not need to return anything

2 Kinds of WordPress Hooks

Actions
Filters

Actions

Actions = Do Something

Filters

Filters = Transform

Actions and Filters

Actions = Do Something
Filters = Transform

MOAR KITTEHS

kthxbai
Beginning WordPress
Plugin Development
Aizat Faiz aizat.faiz@gmail.com
http://blog.aizatto.com/?p=3729

S-ar putea să vă placă și