This document discusses WordPress plugins and database tables. It covers hooks, which allow plugins to connect functions to events. Hooks are either actions or filters. Database tables in WordPress use a metadata structure, with meta tables containing key-value pairs that provide flexibility. Taxonomies classify terms and allow terms to be associated with posts through term relationships and term taxonomies. Next week's topics will include adding admin menus and handling POST data for plugins.
4. Recap
Plugin Header:
/*
Plugin Name: Hello Dolly
Plugin URI: http://wordpress.org/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an
entire generation summed up in two words sung most famously by Louis Armstrong: Hello,
Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in
the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.6
Author URI: http://ma.tt/
*/
13. Task Separation
Q. I want to add extra contents to every post to be
displayed. Which one do I have to use?
Q. I want to a add menu page to admin page. Which one?
Q. I want to customize the table columns of pages screen.
Q. I want to do something when a post is updated.
14. Task Separation (solution)
Q. I want to add extra contents to every post to be
displayed. Which one do I have to use?
Q. I want to a add menu page to admin page. Which one?
Q. I want to customize the table columns of pages screen.
Q. I want to do something when a post is updated.
A. filter. ‘the_content’ hook.
A. action. ‘admin_menu’ hook.
A. filter. ‘manage_pages_columns’ hook.
A. action. ‘post_updated’ hook.
15. Task separation
Do not have to remember them all.
Moreover, actually,
add_action == add_filter
16. Action, Filter definition
wp-includes/plugin.php
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
add_action() is mere an alias of add_filter()
Then, why add_action() is created?
Because they want to get more detailed code.
They just wanted detailed conception. That’s cool.
23. callback functions...
....
..
.
my_hook - my_callback
$wp_filter
add_action(
‘my_hook’,
‘my_callback’,
10,
1
);
somewhere in your code
...
do_action(
‘my_hook’
);
...
by my_hook:
my_callback()
24. do_action
inside of do_action:
● retrieve the callback functions by their tags (hook)
● sort by priority
● call_user_func()
do {
foreach ( (array) current($wp_filter[$tag]) as $the_ )
if ( !is_null($the_['function']) )
call_user_func_array($the_['function'],
array_slice($args, 0, (int) $the_['accepted_args']));
} while ( next($wp_filter[$tag]) !== false );
26. Check it out!
Build a plugin that dumps all registered
actions or filters.
Add your own hook and call it.
Download template code and edit.
http://<myip>/wp-admin/ (meetup / 1)
ftp://<myip> (meetup / 1)
27. Summary
● filter and action are the same. action is alias.
o structure, data separation
● In fact, this is like a fine wrapper for call_user_func.
o do_action() fires callback functions when the hook
is available
o add_action() loads callback functions.
● There are huge amount of hooks: see reference.
● You can also define your own hooks.
36. Hashing
key - value pair.
key is short / value is often long.
key is searchable.
Hashing often called (implemented) as
“dictionary”, or “associative array”.
37. Meta key: benefits
Flexible, extendable
Ordinary tables can’t do these:
● Creating fields “on demands”
● This is one reason why WordPress is
a very versaitle, general, and
easy-to-customize CMS.
38. Meta key sample: user info
You want to add “Kakaotalk ID”
for every user:
just add “kakaotalk_id” meta key.
54. Initial Taxonomies
< wp-includes/taxonomy.php >
function register_taxonomy(
$taxonomy, $object_type, $args = array() ) { ... }
function create_initial_taxonomies() {
register_taxonomy( 'category', 'post', array(...) );
register_taxonomy( 'post_tag', 'post', array(...) );
... and so on ...
create_initial_taxonomies() is called in wp-settings.php
55. Summary
● Wordpress Datbase table uses metadata
style strategy
● meta tables have key / value fields
o flexible, easy to extend
o while it can have some disadvantages
56. Summary
● Taxonomy: classification of term
● You can add taxonomies (as well as terms)
● Taxonomy can be flat or hierarchical
● term_taxonomy: taxonomy -
term
● term_relationship: posts - taxonomy
57. Next week
Admin Menu
● Almost every plugin has its menu
● Easy WP Plugin API
Admin Post
● POST type data submission
URL Query
AJAX Data handling
Wordpress Posts in Nutshell