WordPress custom posts allow creating custom post types beyond standard posts and pages. This presentation demonstrates how to create a custom post type for product catalogs using post meta boxes and custom fields. It covers registering a custom post type, adding meta boxes to the post editor interface, and saving custom field values to the database on post update. The example creates a "Grills" post type with fields for product details, images, and specifications to structure data entry for a grill catalog site.
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Create Custom Post Type Plugin
1. WORDPRESS CUSTOM POSTS
USING POST META BOXES
CHICAGO NORTHSIDE WORDPRESS MEETUP,
2/4/14
This presentation at:
http://bigwdesign.com/presentations/wp-custpost-coded.pdf
2. WHAT IS A CUSTOM POST?
NOTHING BUT AN ENTRY IN THE WP_POSTS
TABLE WITH A SPECIAL POST_TYPE TO
DISTINGUISH IT FROM OTHER POST TYPES.
And in our case, some associated wp_postmeta entries.
3. WHY USE CUSTOM POSTS?
You need a post type other than post or page.
Structured way for editors/users to consistently enter data.
Format posts consistently using a custom template.
You'd like to classify and access posts using some custom taxonomy.
4. EXAMPLE: GRILLS, INC.
Wants to implement a special product catalog site.
4 products now; 400 in a few years.
Site admin could create HTML and copy it indefinitely.
Prone to error, requires good HTML skills, hard for users.
Need simple, easy-to-use, structured input method, and a common
look.
Decision: Custom Post and template.
5. CODING APPROACH
Use of meta boxes for custom data ... as opposed to custom fields.
(Ease of creation vs. performance?)
It all ends up in wp_postmeta anyhow
Use add_action hooks into Wordpress: Hook a function on to a
specific WP action.
Use add_filters: Hook a function to a specific filter (run
before/modify a WP action).
6. PRESENTATION CODING TASKS
New plugin directory in wp-content/plugins (e.g., custom_grills)
A grills.php plugin file - nothing but a collection of functions
Admin post metaboxes to enter and save structured data
A single post template to display posts, single-grills.php
A hook into categories
Multi post template for categories, category-grills.php
7. BASIC PLUGIN STRUCTURE
Plugin Header - looks like comments
An add_action hook to run custom post registration function
Function to run register_post_type
Various function parameters
<pp
?h
/
*
Pui Nm:G il
lgn ae rls
Dsrp in C et a dmiti cso gilp ss
ecito: rae n an an u tm rl ot.
Vrin 10
eso: .
Ato: TryB
uhr er .
*
/
adat o('nt,' raegil) / eeue cet _rl weee aypg i gnrtd
d_cin i i' cet_ rl'; / xcts raegil hnv r n ae s eea e
fnto cet _rl( {
ucin raegil )
rgs e_ottp( 'rls,/ tl W w h v ti nwp s tp
eitrps _ye gil ' / el P e ae hs e ot ye
/ ec-pr mtr hr
/ t
aaees ee
8. COMPLETE CUSTOM POST TYPE CREATION
<pp
?h
/
*
Pui Nm:Gil
lgn ae rls
Dsrpin Cet admi ti c so gilpss
eci to: rae n anan utm rl ot.
Vrin 10
eso : .
Ato:Try B
uhr er .
*
/
adat o('nt,' raegil)
d_cin i i' cet_ rl';
fnto cet _rl( {
ucin raegil )
rgse_o ttp( 'rl s,
ei trps_ye gil'
a ry
ra(
'aes =
lbl ' >
ary
ra (
'a e
nm'
= 'rl s, 'ottp gnr lnm'
> Gil' p s ye eea ae ,
'i glrnm'
snua_ ae
= 'rl s, 'ottp snu a nm '
> Gil' p s ye iglr ae,
'd _e'
adnw
= 'd Nw,
> Ad e'
'd _e_tm
adnwi e'
= 'd NwGi l,
> Ad e rl'
'd tie'
ei_tm
= 'dt Gil,
> Ei rl'
'e _tm
nwie'
= 'e Gil,
> Nw rl'
'l _tm'
alies
= 'l Gil'
> Al rls,
'i wie'
ve_tm
= 'iw Gil,
> Ve rl'
'e rhies
sac_t m'
= 'er hGil'
> Sac rl s,
'o _on'
ntfud
= 'og il f ud,
> N rls on'
'o _on_nta h = 'og il i teT ah,
ntfud i_rs' > N rls n h rs'
'a etie_oo ' = ',
prn_t mcln
> '
'a et
prn'
= 'rl s
> Gil'
)
,
'hwi_a_ eu' = TU,
so_ nnvmns
> RE
'irrhcl = tu,
hea cia' > r e
'ecito' = ' od o rpout adpo utseii dt'
dsr pin
> H l s u r d c s n r d c p c f c a a,
'ulc
pbi '
= t u,
> re
'eupsto '= 5
mn_ oiin > ,
'uprs
spo t'
= a ry ' il' 'dtr ,' aeatiue ')
> ra( tte, ei o' pg-t rbts ,
'a_rhv' = t u
hsa cie
> re
)
)
;
}/ edfnt o
/ n ucin
?
>
9. DEMO 1. BASIC CUSTOM POST PLUGIN
Create the directory
Create grills.php
Add the basic grills.php code to wp-plugins
Add & activate plugin in WP back end
Create a custom post for grill #1, & test
Outcome: That's somewhat underwhelming.
10. ADD META BOXES TO BACK END
1. Admin_init function to add meta boxes to admin
2. Add function & HTML to display meta boxes
3. Add action to save contents of meta boxes
11. ADDING META BOXES
1. ADMIN_INIT FUNCTION TO ADD META BOXES
/ ato wl fr o ii o amn
/ ci n il ie n nt f di
adat o('di_n t,'yamn )
d_cin a mnii' m _di' ;
fnto m_d i( {
ucin yamn)
ad mt_o('r l_eabx,
d_eab x gilmt _o'
'rl Dtis,
Gil eal '
'ipa_ut mmt_o'
dsl ycso_ea bx,
'rls,'o ml,'ih
gil ' nra' hg'
)
;
}
12. ADDING META BOXES
2. ADD FUNCTION AND HTML TO DISPLAY
META BOXES
/ fnto wl dslymt -oe o b c ed
/ uc in i l ip a eabxs n ak n
fnto dsl ycso_ea bx $ rl ){
ucin ipa_ut mmt_o( gil
/ Rtiv vro sfed bsdo I
/ eree aiu il s ae n D
$u tmsre =echm (gtps_ ea $ rl-I,'e isnm' tu ))
cso_e is
s _ t l e _ o t m t ( g i l > D s r e _ a e, r e
;
$u tmmdl=e chm(gtp s_ea $rl-I ,'oe_ae ,tu ))
cso_o e
s_tl e_otm t( g il>D md lnm' re
;
/e c
/t
<a l>
tbe
<r
t>
< dsye "it:10" Sre <t>
t tl=wdh 0% >ei s / d
< d<nu tp = tx"sz = 8"nm =sr e_ae vle "?h eh $utm sre;?"
t>ipt ye"et ie"0 a e"eisnm " au = <pp co cso_eis >
<t>
/r
<r
t>
< dsye "it:10" Mdl/d
t tl=wdh 0% >oe < t >
< d<nu tp = tx"sz = 8"nm =md lnm" vle "?h eh $ utmmdl ?"
t > ipt ye"et ie"0 a e"oe_ae au=<pp co cso_ oe; >
<t>
/r
13. COMPLETE ADMIN METABOX FUNCTION
/ fnto wl dslymt -oe o b c ed
/ uc in i l ip a eabxs n ak n
fnto dsl ycso_ea bx $ rl ){
ucin ipa_ut mmt_o( gil
/ Rtiv vro sfld bsdo I
/ eree aiu ie s ae n D
$u tmsre =echm (gtps_ ea $ rl-I,'e isnm' tu ))
cso_e is
s _ t l e _ o t m t ( g i l > D s r e _ a e, r e
;
$u tmmdl=e chm(gtp s_ea $rl-I ,'oe_ae ,tu ))
cso_o e
s_tl e_otm t( g il>D md lnm' re
;
$i lgahc=e chm(gtp s_ea $rl-I ,'ilgah c,tu ))
tterp i
s_tl e_otm t( g il>D tt erpi' r e
;
$u tmiae=e chm(gtp s_ea $rl-I ,'utmia e,tu ))
cso_m g
s_tl e_otm t( g il>D cs o_mg' r e
;
$m g_ato =echm (gtps_ ea $ rl-I,'m g_ato' tu ))
iaecp in
s_tl e_ otmt( gil> D iaecp in, re
;
$o kn_ufc =ech m(gtps _ea $rl-I,' okn_ufc ' tu ))
coigs rae
s_tl e _otmt( gil >D coig srae, re
;
$t _rmr =echm( gtps_e a $r l-I,'t_ rmr' tu ))
bupia y
s _tl e_o tmt( gil>D bupiay, re
;
$t _eodr =echm (gtps_ ea $ rl-I,'t _eodr' tu ))
buscn ay
s_tl e_ otmt( gil> D buscn ay, re
;
$u _u = echm(gt ps_ea $rl- I,'u_u' tu ))
ctot
s_t l e_otm t( gil>D c tot, re
;
$i eunr=ec hm(gtps _ea $rl-I,'ieunr ,tu ) )
sdbre
s_tl e_otmt ( gi l>D sdb re' re
;
$x ep = echm(gt ps_ea $rl- I,'xep' tu ))
ecrt
s_t l e_otm t( gil>D e crt, re
;
?
>
<a l>
tbe
<r
t>
< dsye "it:10" Sre <t>
t tl=wdh 0% >ei s / d
< d<nu tp = tx"sz = 8"nm =sr e_ae vle "?h eh $utm sre;?"
t>ipt ye"et ie"0 a e"eisnm " au = <pp co cso_eis >
<t>
/r
<r
t>
< dsye "it:10" Mdl/d
t tl=wdh 0% >oe < t >
< d<nu tp = tx"sz = 8"nm =md lnm" vle "?h eh $ utmmdl ?"
t > ipt ye"et ie"0 a e"oe_ae au=<pp co cso_ oe; >
<t>
/r
<r
t>
< dsye "it:10" TteGah c /d
t tl=wdh 0% >il rpi < t >
< d<n u t p = t x " s z = 8 "n m =t t e r p i " v l e" ? h e h $ i l g a h c ? "
t > ipt ye"et ie"0 a e"ilgah c au=<p p co tte rpi; >
<t>
/r
<r
t>
14. ADDING META BOXES
3. ADD ACTION TO SAVE META
BOXES/FIELDS
/ fnto wl ad dt t w_otea o sv _ot I ps _ye=gil
/ uc in i l d aa o pps mt n aeps, F ottp
rls
adat o('aeps ' 'd _rl_il s,1,2) / pirt &ag
d_cin s v_ot, adgil fed' 0
; / ro iy
rs
fnto adg ilfed($ rl_d $rl ){
ucin d_rl_i ls gili , g il
/ Cekp s tp frg il
/ hc ot ye o rls
i ($rl -ps_ye= 'rl s ) {
f
gil>ot tp = gil'
/ Soed t i ps mt tbei peeti ps dt
/ tr aa n ot e a al f rsn n ot aa
i ( i s t $ P S [ s r e _ a e] ) & $ P S [ s r e _ a e ] ! ' ) {
f
se( _OT ' eisnm'
& _OT'eis nm' = '
udt_o tmt($rl _d 'eisnm' $PS [sre_ae])
p aeps_ea gili, sre _ae, _OT'ei snm' ;
}
/ ec
/ t
15. COMPLETE META BOX/FIELD SAVE
FUNCTION
/ adsv_o tato t a dgilfe d it ps_ ea
/ d aeps ci n o d rl ils no otmt
a d a t o ( ' a e p s ' ' d _ r l _ i l s, 1 , 2)
d_cin s v_ot, adgil fed' 0
;
fnto adg ilfed($ rl_d $rl ){
ucin d_rl_i ls gili, g il
/ Cekp s tp frg il
/ hc ot ye o rls
i ($rl -ps_ye= 'rl s ) {
f
g i l> o t t p = g i l '
/ So edt i ps mt tbe i pe eti ps d t
/ tr aa n ot ea al f rsn n ot aa
i ( i s t $ P S[ s r e _ a e] ) & $ P S [ s r e _ a e ] ! ' ) {
f
se( _OT' eisnm'
& _OT'eis nm' = '
udt_o tmt($rl _d 'eisnm' $PS[sre_ae])
p aeps_ea gili, sre _ae, _OT'ei snm' ;
}
i ( i s t $ P S[ m d l n m ' ) & $ P S[ m d l n m ' ! ' ) {
f
se( _OT' oe_ae]
& _OT' oe_ae] = '
udt_o tmt( $rl _d 'oe_ae , $PS [mdl nm' )
p aeps_ea gili, mdl nm' _OT 'oe_ae ] ;
}
i ( i s t $ P S[ t t e r p i ' ) & $ P S [t t e r p i ' ! ' ) {
f
se( _OT' ilgahc]
& _OT 'ilgahc ] = '
udt_o tmt( $rl _d 'ilgahc , $PS [tt erpi' )
p aeps_ea gili, tte rpi' _O T'ilgah c] ;
}
i ( i s t $ P S[ c s o _ m g ' ) & $ P S [c s o _ m g ' ! ' ) {
f
se( _OT' utmiae]
& _OT 'utmiae ] = '
udt_o tmt( $rl _d 'utmiae , $PS [cs o_mg' )
p aeps_ea gili, cso _mg' _O T'utmia e] ;
}
i ( i s t $ P S[ i a e c p i n] ) & $ P S [ i a e c p i n] ! ' ) {
f
se( _OT' mg_ato '
& _OT'mg _ato' = '
u d t _ o t m t ( $ r l _ d ' m g _ a t o ' $ P S[ i a e c p i n] )
p aeps_ea gili, iae cpin , _OT'm g_ato' ;
}
i ( i s t $ P S[ c o i g s r a e] ) & $ P S[ c o i g s r a e] ! ' ) {
f
se( _OT' okn_uf c'
& _OT'o kn_ufc' = '
u d t _ o t m t ( $ r l _ d ' o k n _ u f c ' $ P S[ c o i g s r a e] )
p aeps_ea gili, coi gsrae , _OT' okn_ufc ' ;
}
i ( i s t $ P S[ b u p i a y] ) & $ P S [ b u p i a y ] ! ' ) {
f
se( _OT' t_rmr'
& _OT't_r mr' = '
udt_o tmt($rl _d 't_rmr' $PS[bupiay])
p aeps_ea gili, bup iay, _OT't_ rmr' ;
}
i ( i s t $ P S[ b u s c n a y] ) & $ P S [ b u s c n a y] ! ' ) {
f
se( _OT' t_eodr '
& _OT't_ eodr' = '
u d t _ o t m t ( $ r l _ d ' t _ e o d r ' $ P S[ b u s c n a y] )
p aeps_ea gili, bus cnay , _OT't _eodr' ;
}
i ( i s t $ P S[ c t o t ] ) & $ P S [c t o t ] ! ' ) {
f
se( _OT' u_u'
& _O T'u_u' = '
udt_o tmt( $rl _d 'u_u' $PS [c tot ])
p aeps_ea gili, cto t, _ OT'u_u' ;
}
i ( i s t $ P S[ s d b r e ' ) & $ P S[ s d b r e ' ! ' ) {
f
se( _OT' ieunr]
& _OT' ieunr] = '
udt_o tmt( $rl _d 'ieunr , $PS [sdb re' )
p aeps_ea gili, sdb re' _OT 'ieunr ] ;
}
i ( i s t $ P S[ e c r t ] ) & $ P S [e c r t ] ! ' ) {
f
se( _OT' xep'
& _O T'xep' = '
udt_o tmt( $rl _d 'xep' $PS [e crt ])
p aeps_ea gili, ecr t, _ OT'xep' ;
}
}
}
16. DEMO 2. METABOXES
Add the three Meta Box functions to grills.php
Add some data to grill #1
Create grill #2 and add data to it as well
Outcome: Metaboxes added to back end, and we are saving custom
post data
17. CUSTOM SINGLE TEMPLATE
1. Add action to register special style sheet
2. Add filter to use special template
3. Create single item template
18. CREATING CUSTOM SINGLE TEMPLATE
1. ADD ACTION TO REGISTER SPECIAL STYLE
SHEET
/ rgse tecsw aeg igt uewt orcso tml t
/ ei tr h s e r on o s ih u utm epae
adat o('peqe esrps,' eitrgi lsye')
d_cin w _nuu_ci t' rgs e_rl_tl s ;
fnto rgs e_rl_tl s){
ucin eitrgi lsye(
w_ eitrsye 'ut mgil' puis ul ' cso_rls csgilsy ecs ))
prgse _tl( cso_rl s, lgn_r ( /utm gil/s/r l-tl.s'
;
w_ nuu_tl( 'utmgil ')
peqee sye cso _rls ;
}
19. CREATING CUSTOM SINGLE TEMPLATE
2. ADD FILTER TO USE SPECIAL TEMPLATE
/ freueo ddc tdtmlt
/ oc s f eiae e pae
adfl e('iget mlt' 'e_ut mps_yet mlt')
d_itr s nl_epae , gt cso_ot tp_epae ;
fnto gtc so_ottp _epae$ igetmlt){
ucin e_utmp s_yetml t( snl_ epae
goa $ot
l bl p s;
i (ps-ps_ ye= 'rls ){
f $ot >ottp = gil '
$ i g e t m l t = d r a e _ F L _ ) . ' s n l - r l s p p;
sn l_epae
inm ( _ IE_
/igeg il.h'
}
/ eh " snl_epa e;
/ co $ige tmlt"
/ de)
/ i(;
rtr $iget mlt;
e un s nl_epae
}
20. CREATING CUSTOM SINGLE TEMPLATE
3. CREATE SINGLE ITEM TEMPLATE - COPY &
MODIFY THEME'S SINGLE.PHP
(JUST LIKE BUILDING A STANDARD WORDPRESS
TEMPLATE, BUT WITH YOUR CUSTOM LAYOUT AND
DATA)
<pp
?h
gtha e(;?
e_edr) >
<i i =pia y >
dv d "rmr"
<i i =c net rl = mi"
d v d" o t n " o e " a n>
<p p
?h
/ $yot =ary 'ottp' = ' utmpout ' )
/ mps
ra ( ps _ye > cso_ rdcs, ;
/ $op= nwW_ur( $yot)
/ lo
e P Qey mps ;
?
>
<p p/ w ie($op> aepss) ):$op>h _ot)
?h / hl
lo-hv_o t(
l o-teps( ;
wie (hv_ot( ):t eps(;?
hl
ae pss)
h_o t) >
/ ec
/ t
21. DEMO 3. CUSTOM TEMPLATE
Add the code to grills.php, create single-grills.php
Note that our custom post now shows in a custom template
22. MIGHT WANT TO VIEW MORE THAN ONE
PRODUCT FROM MENU SELECTION
Create a Custom taxonomy (another day)
Add custom post type to existing Category system
23. ADDING A CUSTOM POST TYPE TO EXISTING
CATEGORY SYSTEM
1. Modify register_post to include taxonomy Category
2. Add pre_get_posts action that will add our post type to main
query
3. Create category-grills.php template to display multiple posts
24. ADDING CUSTOM POST TYPE TO
CATEGORIES
1. MODIFY REGISTER_POST TO INCLUDE
TAXONOMY CATEGORY
ADD ONE LINE (TAXONOMIES) TO BOTTOM OF
FUNCTION CREATE_GRILL
/ ecec
/ t t
'eupsto '= 5
mn_ oiin > ,
'uprs
spo t'
= a r y ' i l' ' d t r , ' a ea t i u e ' )
> ra( tte , ei o' pg-t rbts ,
'a_rhv ' = t u,
hsa cie
> re
'aoois = ary'ae oy)
txn me' > ra (ctgr'
)
25. ADDING CUSTOM POST TYPE TO
CATEGORIES
2. ADD PRE_GET_POSTS ACTION THAT WILL
ADD POST TYPE TO MAIN QUERY
NEW FUNCTION ADDED TO GRILLS.PHP
/ So psso 'ot,'a e ad 'rls ps tpso hm pg
/ hw ot f ps ' pg' n gi l' o t ye n o e ae
adat o('r_e_ ot' 'd_rl_ ottp_oq ey )
d_cin p egtpss, adg ilps_y et_ur' ;
fnto adg ilps_ye t_ur($ ur ) {
ucin d_rl_o ttp_oqe y qey
i ($ur -i_anqe y ))
f
qey>sm i_ur (
$ur-st 'ott p' a ry 'ot,'ae ,'rls ))
qey >e( ps_ye, ra( ps' pg' gi l'
;
rt r $u r;
eun qey
}
26. ADDING CUSTOM POST TYPE TO
CATEGORIES
3. CREATE CATEGORY-GRILLS.PHP TEMPLATE
TO DISPLAY MULTIPLE POSTS
Copy category.php from main template to our plugin dir, and modify
as needed
Add new filter on archive_template to force use of our archive
template
Add some Wordpress categories in backend, assign posts, make menu
items!
/ freueo ddc tdctgr tml t
/ oc s f eiae a eoy epae
adfl e('aeoy tmlt' 'e_r lsctgr_ epae )
d_itr c tgr_epa e, g tgil_a eoytmlt ' ;
fnto gtg il_aeoy tmlt(c tgr_epa e {
ucin e_rlsc tgr_epa e$aeoy tmlt)
goa $ot
lb l ps;
i ( p s - p s _ y e = ' r l s ){
f $ot> ottp = gil'
$ a e o y t m l t = d r a e _ F L _ ). ' c t g r - r l s p p ;
ctg r_epae
inm ( _IE_
/ aeoy gil .h'
}
rtr $aeoyt mlt;
eu n ct gr_epae
}
27. DEMO 4. CUSTOM POSTS BY CATEGORY
Modify main function to add taxonomy
Add function to include grills in posts query
Add function to force use of category-grills template
Add some special grill categories to back end and assign
posts
Menu items to those categories
Outcome: Menu items display categories of custom posts
28. THE END
Presentation originally developed from tutorials at:
http://wp.tutsplus.com/tutorials/plugins/a-guide-towordpress-custom-post-types-creation-display-and-metaboxes/
http://wp.tutsplus.com/tutorials/plugins/a-guide-towordpress-custom-post-types-taxonomies-admin-columnsfilters-and-archives/
See also Wordpress codex on post_types (has many other links):
http://codex.wordpress.org/Post_Types
This presentation:
http://bigwdesign.com/presentations/wp-custpost-coded.pdf