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
github.com/rexrana/my-custom-structure
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, Meta Box, Toolset)
-
Most have an export to code functionality
-
Developer libraries: CMB2, Carbon Fields
-
Code Generators: GenerateWP, WP-CLI scaffold command
GUI-based structure plugins
-
Create your structure in the admin dashboard.
- optionally, export to code
- Advanced Custom Fields
- Pods framework
- Meta Box
- Toolset
- most popular plugin for creating custom fields
- free version very LIMITED - requires Pro version (subscription) for :
- repeating field groups
- Gutenberg block support
- more field types
- export to code - JSON and PHP formats
- Can create Content Types and Fields
- support for block editor (Gutenberg) and Classic Editor
- create advanced relationships
- includes REST API and WP-CLI commands
- Translation Ready - integrations with WPML, Polylang
- lightweight, fast and simple to use
- multisite support
- 40+ field types
- includes online generators for fields, post types, taxonomies and blocks
- Extensive support for block editor, including a library of dynamic blocks
- Types: Custom Post Types, Taxonomies and Fields
- create templates using block editor
- Forms builder
- Access: manage roles and privileges
- Maps: Google Maps or Azure Maps
Developer libraries
- Declare metaboxes, fields in code using defined API
- you can track changes in git within your plugin
include()
the library in PHP, or use Composer to delcare as plugin dependency
-
Toolkit for building metaboxes, custom fields, and forms
-
Declare metaboxes and fields using arrays of parameters
-
35 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-friendly way of declaring custom fields
- Feature rich out-of-the-box (no paid "pro" version)
- custom fields for post types, options pages, taxonomies, users, comments, navigation menus, and widgets.
- repeatable field groups
- define dynamic Gutenberg blocks without JavaScript.
Code Generators
- online forms or command-line tools
- generates the code for you
- copy / paste into php file in your plugin
-
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.
- Command for WordPress command-line interface (WP-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 root directory
Post Type: Film
wp scaffold post-type film --label=Film --textdomain=structure \
--dashicon=editor-video --plugin=my-custom-structure
Taxonomy: Genre
wp scaffold taxonomy genre --post_types=film --label=Genre \
--textdomain=structure --plugin=my-custom-structure
- GUI based plugins: install plugin in normal way
- Developer libraries: usually need to do a PHP
include()
, or declare as Composer dependency for your plugin
-
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
CMB2:
if( file_exists( dirname(__FILE__).'/lib/cmb2/init.php' ) ) {
require_once( 'lib/cmb2/init.php' );
}
Carbon Fields (using Composer):
composer require htmlburger/carbon-fields
Make sure to include Composer's PSR-4 autoloader in plugin
require_once (__DIR__ . '/vendor/autoload.php');
Includes for declaring structure
// Post Types / Taxonomies.
require_once('post-types/film.php');
require_once('taxonomies/genre.php');
// METABOXES AND FIELDS.
/* using WordPress core functionality */
require_once('inc/wp-fields.php');
/* using ACF */
require_once('inc/acf-fields.php');
/* using CMB2 */
require_once('inc/cmb2-metaboxes.php');