Download - WordPress plugin #2
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/
*/
Recap
Callback Mechanism:
add_action( ‘admin_notices’, ‘hello_dolly’ );
function hello_dolly() {...
}
Name of hook
Callback function
WP function for hook
Hello dolly’s case2 actions: admin_notices, admin_head
● admin_notices codex● admin_head codex
Hello dolly’s case2 actions: admin_notices, admin_headLet’s find source code.
$ fgrep -Rn do_action\(\ \'admin_notices\'\ \) *wp-admin/admin-header.php:238: do_action( 'admin_notices' );
$ fgrep -Rn do_action\(\ \'admin_head\'\ \) *wp-admin/includes/media.php:491: do_action( 'admin_head' );wp-admin/includes/template.php:1627:do_action( 'admin_head' );wp-admin/includes/class-wp-press-this.php:1267: do_action( 'admin_head' );wp-admin/admin-header.php:125:do_action( 'admin_head' );
show source codes
Hook interfacewp-includes/plugin.php
What is action?What is filter?
● add_filter()
● has_filter()
● apply_filters()
● add_action()
● has_action()
● do_action()
Task Separation
Themes: visual partPlugins: functional part
Actions: structural partFilters: information part
Task SeparationQ. 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.
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.
Action, Filter definitionwp-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.
do_action, add_action feedback
do_action:fires registered action
add_action:loads any actions
No restriction. you can define your own hooks.
callback functions..........my_hook - my_callback
$wp_filter
add_action( ‘my_hook’, ‘my_callback’, 10, 1);
callback functions..........my_hook - my_callback
$wp_filter
add_action( ‘my_hook’, ‘my_callback’, 10, 1);somewhere in your code
...
do_action( ‘my_hook’);
...
callback functions..........my_hook - my_callback
$wp_filter
add_action( ‘my_hook’, ‘my_callback’, 10, 1);somewhere in your code
...
do_action( ‘my_hook’);
...
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()
do_actioninside 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 );
add_actionfunction add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { global $wp_filter, $merged_filters;
$idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add,
'accepted_args' => $accepted_args); unset( $merged_filters[ $tag ] ); return true;}
$wp_filter is an array.
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)
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 availableo add_action() loads callback functions.
● There are huge amount of hooks: see reference.● You can also define your own hooks.
Database: the Root
Database: backbone of web apps.
A good app without understanding its db structure: just an illusion.
Database description
https://codex.wordpress.org/Database_Description
● parent - meta styles
o wp_comments - wp_commentmeta
o wp_user - wp_usermeta
o wp_posts - wp_postmeta
o wp_options
● wp_terms - wp_term_taxonomy - wp_term_relationships
Hashing
key - value pair.key is short / value is often long.
key is searchable.
Hashing often called (implemented) as “dictionary”, or “associative array”.
Meta key: benefits
Flexible, extendableOrdinary 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.
Meta key sample: user info
You want to add “Kakaotalk ID” for every user:
just add “kakaotalk_id” meta key.
Does it matter?
Yes! When fresh install,
WordPress: 11+ tables
74 tables
49 tables
79 tables
Jomla:
Drupal:
XpressEngine:
Meta key: Hazards
No type checking, validationMeta key consistencyEfficiency
Nothing is perfect!
next: terms, taxonomies, ...
Adding Terms in Posts
Posts have basically two taxonomies:● tags● categories
Adding tag/category = adding term
Adding Terms in Posts
<term_taxonomy>term_taxonomy_id: 2term_id: 2taxonomy: category / post_tagdescription:parent: 0count: 0
Post ID: 75 term_relationships.object_id term_relationships.term_taxonomy_id
term_taxonomy.term_taxonomy_idterm_taxonomy.term_idterm_taxonomy.taxonomy ★
Post ID: 75 term_relationships.object_id term_relationships.term_taxonomy_id
term_taxonomy.term_taxonomy_idterm_taxonomy.term_idterm_taxonomy.taxonomy ★
Post ID: 75 term_relationships.object_id term_relationships.term_taxonomy_id
terms.term_idterms.name ★terms.slug
term_taxonomy.term_taxonomy_idterm_taxonomy.term_idterm_taxonomy.taxonomy ★
Post ID: 75 term_relationships.object_id term_relationships.term_taxonomy_id
terms.term_idterms.name ★terms.slug
term_taxonomy.term_taxonomy_idterm_taxonomy.term_idterm_taxonomy.taxonomy ★
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
Summary
● Wordpress Datbase table uses metadata style strategy
● meta tables have key / value fieldso flexible, easy to extendo while it can have some disadvantages
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