
Do it in code!
A guide to creating a custom site structure plugin in WordPress.
Presented by:
Peter Hebert
Rex Rana Design and Development Ltd.
What is site structure?
- Post Types
- Metaboxes and Fields
- Taxonomy
- Menus
- Permalink Structure
Why a custom plugin?
- Functionality should live in code, not the database
- Themes for presentation, plugins for functionality
- Manage code in version control system (git, svn)
- Code Portability / Deployment
- dev / staging / production
- re-use for multiple websites / different clients
Our Sample Plugin
We'll create a Film listing (like IMDB)
- Custom Post Type: Film
- Custom Taxonomy: Genre
- Metabox: Film Info
- Fields: Release Date, Duration, Director
WordPress core functionality
- Not easy to use — you have to do all the heavy lifting
- CRUD functions (create, read, update and delete)
- Callback functions with HTML to display fields in admin
- Sanitization / validation of input
- nonces
- It's a lot to remember — time could be better spent
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 Boxes
- Code Generators: GenerateWP, WP-CLI scaffold command
GUI-based structure plugins
Create your structure in the admin, then export to code.
- Advanced Custom Fields
- export to XML (v4 and below), JSON (v5), PHP
- Pods framework
- Migrate: Packages
- Pods Deploy - uses WordPress REST API (beta)
- Toolset
- Embedded Toolset / Packager
Developer library: CMB2
- Toolkit for building metaboxes, custom fields, and forms
- Takes care of all the hard stuff for you
- Declare metaboxes and fields using arrays of parameters
- 32 field types included, with API to declare your own
- Use on Post Types, Options Pages, User Profiles
- even on the front end of your site
Developer library: Custom Meta Boxes
- Framework for easily adding custom fields
- works similarly to CMB2 (parameter arrays)
- (seems to) only work on post edit pages
- fork of original CMB (predecessor to CMB2)
- 20 field types included, plus repeater field
- features a basic 12 column grid system to align fields
Code Generators: GenerateWP
- Form-based wizards for generating code compliant with WordPress coding standards
- Select a tool, fill in the form, generate the code
- Copy ready-to-use code directly to your plugin.
Code Generators: WP-CLI 'scaffold'
Command within WordPress command-line interface (CLI)
- Generates starter code for themes, plugins, unit tests, post types and taxonomy.
- Limited - cannot specify advanced parameters, only gives minimal config with defaults
- Provides a quick start-off point
WP-CLI Scaffolding Examples
Run commands from within site directory
'Film' post type
wp scaffold post-type film --label=Film --textdomain=structure \
--dashicon=editor-video --plugin=my-custom-structure
'Genre' taxonomy
wp scaffold taxonomy genre --post_types=film --label=Genre \
--textdomain=structure --plugin=my-custom-structure
Putting it all together
- If 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 file
- this makes items easier to find, comment out or remove
Developer library includes
Advanced Custom Fields
if( file_exists( dirname(__FILE__).'/lib/advanced-custom-fields/acf.php' ) ) {
include_once( 'lib/advanced-custom-fields/acf.php' );
}
CMB2
if( file_exists( dirname(__FILE__).'/lib/cmb2/init.php' ) ) {
include_once( 'lib/cmb2/init.php' );
}
Includes for declaring structure
include_once('post-types/film.php');
include_once('taxonomies/genre.php');
// METABOXES AND FIELDS
/* using WordPress core functionality */
include_once('inc/wp-fields.php');
/* using ACF */
include_once('inc/acf-fields.php');
/* using CMB2 */
include_once('inc/cmb2-metaboxes.php');
Resources
- CMB2: THE METABOX STRIKES BACK (Justin Sternberg)
storyftw.com/cmb2-metabox-strikes-back - Plugin comparison - Custom Post Types / Fields
(Google spreadsheet) goo.gl/o7kU9s - Custom Post Type Permalinks (plugin)
wordpress.org/plugins/custom-post-type-permalinks/
Thank You
Permalink: slides.rexrana.com/wcyvr2016
Find us online
Rex Rana
Peter Hebert