1
Do it in code!A guide to creating a custom site
structure plugin in WordPress.Presented by:
Peter HebertRex Rana Design and Development Ltd.
Rex Rana
2
What is site structure?Post TypesMetaboxes and FieldsTaxonomyMenusPermalink Structure
Rex Rana
3
Why a custom plugin?Functionality should live in code, not the databaseThemes for presentation, plugins for functionalityManage code in version control system (git, svn)Code Portability / Deployment
dev / staging / productionre-use for multiple websites / different clients
4
Our Sample PluginWe'll create a Film listing (like IMDB)
Custom Post Type: FilmCustom Taxonomy: GenreMetabox: Film InfoFields: Release Date, Duration, Director
https://github.com/rexrana/my-custom-structure
5
WordPress core functionalityNot easy to use — you have to do all the heavy li ing
CRUD functions (create, read, update and delete)Callback functions with HTML to display fields in adminSanitization / validation of inputnonces
It's a lot to remember — time could be better spent
6
Thankfully, there’s a better way!Many tools exist for simplifying the creation of site structure:
Plugins (Advanced Custom Fields, Pods, etc.)Most have an export to code functionality
Developer libraries: CMB2, Custom Meta BoxesCode Generators: GenerateWP, WP-CLI scaffold command
7
GUI-based structure pluginsCreate your structure in the admin, then export to code.
export to XML (v4 and below), JSON (v5), PHP
frameworkMigrate: PackagesPods Deploy - uses WordPress REST API (beta)
/ Packager
Advanced Custom Fields
Pods
ToolsetEmbedded Toolset
8
Developer library: CMB2 github.com/WebDevStudios/CMB2
Toolkit for building metaboxes, custom fields, and formsTakes care of all the hard stuff for youDeclare metaboxes and fields using arrays of parameters32 field types included, with API to declare your ownUse on Post Types, Options Pages, User Profiles
even on the front end of your site
9
Developer library: Custom Meta Boxes github.com/humanmade/Custom-Meta-Boxes/
Framework for easily adding custom fieldsworks similarly to CMB2 (parameter arrays)(seems to) only work on post edit pagesfork of original CMB (predecessor to CMB2)20 field types included, plus repeater fieldfeatures a basic 12 column grid system to align fields
10
Code Generators: GenerateWP generatewp.com
Form-based wizards for generating code compliant withWordPress coding standardsSelect a tool, fill in the form, generate the codeCopy ready-to-use code directly to your plugin.
11
Code Generators: WP-CLI 'scaffold' wp-cli.org/commands/scaffold/
Command within WordPress command-line interface (CLI)Generates starter code for themes, plugins, unit tests, posttypes and taxonomy.Limited - cannot specify advanced parameters, only givesminimal config with defaultsProvides a quick start-off point
12
WP-CLI Scaffolding ExamplesRun commands from within site directory'Film' post typewp scaffold posttype film label=Film textdomain=structure \dashicon=editorvideo plugin=mycustomstructure
'Genre' taxonomywp scaffold taxonomy genre post_types=film label=Genre \textdomain=structure plugin=mycustomstructure
13
Putting it all togetherIf using ACF or CMB2 :
Install plugin in normal way, or:Download to a subdirectory, include in your plugin's main PHP file
Create separate PHP files for each of: Post Types, Taxonomy,Metaboxes/Fields
include these files in your main plugin PHP filethis makes items easier to find, comment out or remove
14
Developer library includesAdvanced Custom Fieldsif( file_exists( dirname(__FILE__).'/lib/advancedcustomfields/acf.php' ) ) include_once( 'lib/advancedcustomfields/acf.php' );
CMB2if( file_exists( dirname(__FILE__).'/lib/cmb2/init.php' ) ) include_once( 'lib/cmb2/init.php' );
15
Includes for declaring structureinclude_once('posttypes/film.php');include_once('taxonomies/genre.php');
// METABOXES AND FIELDS/* using WordPress core functionality */include_once('inc/wpfields.php');/* using ACF */include_once('inc/acffields.php');/* using CMB2 */include_once('inc/cmb2metaboxes.php');
16
ResourcesCMB2: THE METABOX STRIKES BACK (Justin Sternberg)
Plugin comparison - Custom Post Types / Fields(Google spreadsheet) Custom Post Type Permalinks (plugin)
story w.com/cmb2-metabox-strikes-back
goo.gl/o7kU9s
wordpress.org/plugins/custom-post-type-permalinks/
17
Thank YouFind me online:
rexrana.capeterhebert.com@RexRanaDesign@peter_hebert
peterhebertrexrana