2. Recap
Hooks in Nutshell
● Hook is an event
● Plugin is hook-driven
● Task separation
o Changing core's workflow: action
o Modifying contents: filter
o In fact, action and filter are the same
3. Recap
Hooks in Nutshell
● Callback function utility
● You can define your own hooks
● There are many predefined hooks
4. Recap
Datbase Nutshell
● Parent-meta strategy
o Flexiblility
o Extendible
o But can experience some defects
● Meta key, meta value
o Hash-like storage
5. Recap
Database Nutshell
● Term, taxonomy
o Term: a word
o Taxonomy: a classification of words
● There are some pre-defined terms, and
taxonomies
o category (hierarchical), post_tag (flat)
7. Today's Topics
1.Entry Points in Plugin
a. Menu
b. Shortcodes
c. Admin Post
d. Ajax
e. Redirect
f. activation, deactivation, uninstall
2.Managing Your Own Contents
a. All About Custom Posts
9. Entry Points
Plugins run on the server side.
Server only responds when it is requested.
Requested moment → entry point
10. Entry Points
Q. When the server is requested?
A. It is when client access via a URL.
Q. Why a URL is used?
A. To define a resource: output to client
A. To receive data: input from client
11. Entry Points
Q. Output Form?
A. Whatever,
Images, audio, video, html documents, ...
Q. Input form?
A. Generally submitting forms
via GET/POST method.
12. Entry Points
There are some special entry points left for
plugins
● Menu items
● Shortcodes
● Admin post
● AJAX
● Redirecting
● Activation, deactivation, uninstall
13. Menu Items
Callback from admin menu clicking
add_menu_page()
add_submenu_page()
remove_menu_page()
remove_submenu_page()
14. Where is default menu?
wp-admin/menu-header.php
<ul id="adminmenu">
<?php
_wp_menu_output( $menu, $submenu );
function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true )
here menus are printed in html
15. Where is default menu?
wp-admin/menu.php (admin.php includes it)
wp-amdin/includes/menu.php
builds administrative menus
global $menu, $submenu stores menu
information
20. Shortcodes
What is shortcode?
A "magic word" in post content that is
replaced by some process defined in plugin
or themes in runtime.
Good for displaying complex, dynamic
contents
27. Admin-post
But in your own handler,
You cannot use WP core functions.
You cannot authenticate.
Do not reinvent the wheel!
So there is admin-post.php
28. Admin-post
wp-admin/admin-post.php
define & include minimal settings,
and do_action
$action = empty( $_REQUEST['action'] ) ? '' : $_REQUEST['action'];
admin_post_nopriv, admin_post_nopriv_{$action}
admin_post, admin_post_{$action}
finish using die() in callback function!
29. AJAX
Very similar to admin-post, but 'DOING_AJAX' is defined:
define( 'DOING_AJAX', true );
also use die() in callback function
if ( is_user_logged_in() ) {
do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
// Default status
die( '0' );
30. Redirect
You can generate custom page in plugin, by
using 'template_redirect' action.
function my_page_template_redirect() {
if( is_page( 'goodies' ) && ! is_user_logged_in() ) {
wp_redirect( home_url( '/signup/' ) );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
31. Redirect
You can generate web pages on-the-fly by
utilizing these 3 hooks.
● rewrite rule
● query parsing
● redirect
Detailed instructions, later
33. Summary
● menu items, shortcodes, admin post, ajax,
template redirects, and (de)activation-
uninstall hooks are good initial entry
points.
o It is a good strategy to find these
hooks in plugins first and dissect
their codes from those points.
34. I know you want to do something!
You understand hook concept.
You know database detail.
Now you know entry points
Definitely, you wanna make something.
36. 소스코드: http://[ip]/source-codes/
1.어드민 화면에 메뉴 추가
2.쇼트코드 추가
3.메뉴에서 admin post 전송
4.메뉴에서 ajax 전송
5.redirect 테스트
<author> 부분은 자신의 이름으로 일괄변경하고,
적절히 콜백 함수를 정의해 주세요.
Source code explained
41. Arguments are too complex!
Just use Types, or Pods plugin.
Thats' all, problem solved!
Yeah!
42. ... of course, you might not want
this. I was just kidding.
43. Custom Post
You want to manage your own contents.
That's why custom post is.
● Music collections
● Online store
● Event calendar
● Book data
● Photo portfolio
44. Custom Post: Motivation
Well, with just one type "post", and you may
define many categories and use them to
organize all contents.
JUST ONE TYPE: POST
MANY CATEGORIES:
portfolio, calendar, music db, ...
45. Custom Post: Motivation
But you might feel that some post types:
● should be hidden from some users
● even might be excluded from search
● may be only for private use
● may have very complex taxonomies.
so that it should have its own
territory.
● can be accessible from their own URL
rules
46. Custom Post: Motivation
Why re-invent wheel again?
By extending built-in post type system,
you can REUSE whole facilities related to
posts easily!
UI, and all thing about managing
48. register_post_type
<?php register_post_type( $post_type, $args ); ?>
$arg is very complex.
But I think it should be explained in detail. Actually,
you cannot avoid it, because eventuall all post type
plugins will use that function, too.
Those plugins also have diffcult UIs, too.
49. register_post_type
Let's take a look how Types, and Pods create
their custom posts...
They have easy (really?) UI and provide
better functionaliy, but you can see
horribly many options there.
53. register_post_type $args
labels: more specific text labels
name
sigular_name
menu_name
name_admin_bar
all_items
add_new
add_new_item
edit_item
view_item
search_items
search_items
not_found
not_found in trash
parent_item_colon
59. register_post_type $args
show_in_menu:
show admin_menu. show_ui must be true.
Just showing menu.
true, false, else some strings
(tools.php / edit.php)
altough it is false,
you can access URLS like:
/wp-admin/edit.php?post_type=music_collection
(show_ui must be true!)
62. register_post_type $args
capabilities:
to give specific names to each capability
map_meta_cap:
if true, default meta capability will be added
and then, your role or account need capabilities
67. Practice Custom Post
Create custom-plugin-<author>.php
Create a simple custom post.
Display admin menu,
Hide admin_bar,
Write your post:
● Any content is OK.
● Include at least one meta field.
68. Next Week.
Customizing Visual Components
● List Table Screen
● Widgets
● ...
WordPress System Undercover
● Constants and Globals
● WPDB