0% found this document useful (0 votes)
560 views

Beginning WordPress Plugin Development

The document discusses WordPress plugin development. It introduces the two main types of hooks in WordPress: actions, which are hooks that allow plugins to execute functions at specific points, and filters, which allow plugins to modify data by passing it through functions. It provides examples of using common hooks like the_content and init to modify content and enqueue stylesheets. Resources for finding hooks and writing plugins are also referenced.

Uploaded by

anon_198630945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
560 views

Beginning WordPress Plugin Development

The document discusses WordPress plugin development. It introduces the two main types of hooks in WordPress: actions, which are hooks that allow plugins to execute functions at specific points, and filters, which allow plugins to modify data by passing it through functions. It provides examples of using common hooks like the_content and init to modify content and enqueue stylesheets. Resources for finding hooks and writing plugins are also referenced.

Uploaded by

anon_198630945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Beginning WordPress

Plugin Development
Aizat Faiz [email protected]

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

[email protected]

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 [email protected]
http://blog.aizatto.com/?p=3729

You might also like