beginning wordpress plugin development

Download Beginning WordPress Plugin Development

If you can't read please download the document

Upload: aizat-faiz

Post on 16-Apr-2017

17.372 views

Category:

Technology


0 download

TRANSCRIPT

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/

The reason we all use WordPress, is because its free.

By Attribution

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

My slides are also free.Free to use.Free to modify.Free to redistribute.

Aizat Faiz

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

[email protected]

Head developer at UrekaLabsCreate Web applications on Ruby on Rails and PHPUsed to do swing dancing, but stopped a year ago, and have massively gained weight

Your Friends / Resources

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

When developing WordPress plugins, there are several friends you need to know

WordPress Codex

http://codex.wordpress.org

Dirth of information available hereLots of information about everything

WordPress Plugin Directory

Google

http://www.google.com.my

Sometimes its not enoughThe WordPress Codex may not be enough

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/

Actions

Filters

2 Kinds of WordPress Hooks

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 default, and your plugin can add its own filtering

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

Filters

Filters = Transform

Actions and Filters

Actions = Do SomethingFilters = Transform

1133

You are not going to need to know allYou are probably only going to use one or two of them

1133
hooks

* as of WordPress v2.9

You are not going to need to know allYou are probably only going to use one or two of them

Problem:

Finding the Right Hook for the Right Job

There are so many hooks, which one do we need to use?Figure out what you want to doGo through the plugin Action and Filter Reference

Hard to Recommend

Look at the resources I gave youWordPress Codex

WordPress Plugin Directory

Google

Action and Filter Referencehttp://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-pluginreadme.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

http://codex.wordpress.org/Writing_a_Plugin#Readme_Filehttp://wordpress.org/extend/plugins/about/readme.txt

Useful only for publishing toWordPress Plugin Directory

Information about your plugin:Description, Installation, Changelog, Donation Links, Tags, etc...

screenshot-1.png

Useful only for publishing toWordPress Plugin Directory

my-plugin.php

Your Plugin Code

4 parts to a pluginPlugin Header

Hooks

PHP Code

Template Code

File Structure

Plugin Headers

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

Plugin Headers

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

Always on top, no choice

Fill in with your own details

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

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

Hook Name

Callback

Hook Callback

Determines what PHP function to call

Callback can be either:String; or

Array of 2 strings (my preference)

Hook Callback

StringCalls a function

Array of 2 stringsCalls 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 WORDPRESSin a post's content.

Hook Priority

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

Priority

Callback

Priority (optional)

Hook Name

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

Which one goes first?

WordPress2WORDPRESS#the_content

ABolderWordPress#the_content

Default Priority

10

smaller numbers = higher prioritylarger 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 postthat has more than 10 comments

accepted_args

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

accepted_args

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

accepted args (optional)

Hook Name

Callback

Priority

accepted_args

Number of arguments for the filter

Plugin 4

Figure out what you want to do.

I want to add acustom 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

Actions

Filters

2 Kinds of WordPress Hooks

Actions

Actions = Do Something

Filters

Filters = Transform

Actions and Filters

Actions = Do SomethingFilters = Transform

MOAR KITTEHS

Beginning WordPress
Plugin Development

Aizat Faiz [email protected]://blog.aizatto.com/?p=3729

kthxbai