![WordCamp Vancouver 2016](wcyvr2016-logo-white.png)

# 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](https://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, 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](https://www.advancedcustomfields.com/)
    - export to XML (v4 and below), JSON (v5), PHP
- [Pods](http://pods.io/) framework
    - Migrate: Packages
    - Pods Deploy - uses WordPress REST API (beta)
- [Toolset](https://wp-types.com/)
    - [Embedded Toolset](https://wp-types.com/documentation/embedded-toolset/) / Packager

# Developer library: CMB2

[github.com/WebDevStudios/CMB2](https://github.com/WebDevStudios/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

[github.com/humanmade/Custom-Meta-Boxes/](https://github.com/humanmade/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

[generatewp.com](https://generatewp.com/)

- 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'

[wp-cli.org/commands/scaffold/](http://wp-cli.org/commands/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

- cannot specify 'supports' (post types)

# WP-CLI Scaffolding Examples

Run commands from within site directory

'Film' post type

```apache
wp scaffold post-type film --label=Film --textdomain=structure \
--dashicon=editor-video --plugin=my-custom-structure
```



'Genre' taxonomy

```apache
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

- Premium plugins require a ACF Pro licence
- show plugin folder structure in code editor

# Developer library includes

Advanced Custom Fields

```apache
if( file_exists( dirname(__FILE__).'/lib/advanced-custom-fields/acf.php' ) ) {
include_once( 'lib/advanced-custom-fields/acf.php' );
}
```

CMB2

```apache
if( file_exists( dirname(__FILE__).'/lib/cmb2/init.php' ) ) {
    include_once( 'lib/cmb2/init.php' );
}
```

# Includes for declaring structure

```apache
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](http://storyftw.com/cmb2-metabox-strikes-back)
- Plugin comparison - Custom Post Types / Fields  
    (Google spreadsheet) [goo.gl/o7kU9s](https://docs.google.com/spreadsheets/d/1mSqienVYxLopTFGLPK0lGCJst2knKzXDtLQRgwjeBN8/)
- Custom Post Type Permalinks (plugin)  
    [wordpress.org/plugins/custom-post-type-permalinks/](https://wordpress.org/plugins/custom-post-type-permalinks/)

# Thank You

Permalink: [slides.rexrana.com/wcyvr2016](https://slides.rexrana.com/wcyvr2016/#/)

### Find us online



Rex Rana



Peter Hebert





[rexrana.ca](https://rexrana.ca)  




[peterhebert.com](http://peterhebert.com)





[@RexRanaDesign](https://twitter.com/RexRanaDesign)  




[@peter\_hebert](https://twitter.com/peter_hebert)





[rexrana](https://github.com/rexrana)



peterhebert





[rex-rana](https://drupal.org/rex-rana)