do it in code! a guide to creating a custom site structure plugin in wordpress

17
1 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. Rex Rana

Upload: peter-hebert

Post on 19-Jan-2017

26 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 2: Do it in code! A guide to creating a custom site structure plugin in WordPress

2

What is site structure?Post TypesMetaboxes and FieldsTaxonomyMenusPermalink Structure

Rex Rana

Page 3: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 4: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 5: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 6: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 7: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 8: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 9: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 10: Do it in code! A guide to creating a custom site structure plugin in WordPress

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.

Page 11: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 12: Do it in code! A guide to creating a custom site structure plugin in WordPress

12

WP-CLI Scaffolding ExamplesRun commands from within site directory'Film' post typewp scaffold post­type film ­­label=Film ­­textdomain=structure \­­dashicon=editor­video ­­plugin=my­custom­structure

'Genre' taxonomywp scaffold taxonomy genre ­­post_types=film ­­label=Genre \­­textdomain=structure ­­plugin=my­custom­structure

Page 13: Do it in code! A guide to creating a custom site structure plugin in WordPress

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

Page 14: Do it in code! A guide to creating a custom site structure plugin in WordPress

14

Developer library includesAdvanced Custom Fieldsif( file_exists( dirname(__FILE__).'/lib/advanced­custom­fields/acf.php' ) ) include_once( 'lib/advanced­custom­fields/acf.php' );

CMB2if( file_exists( dirname(__FILE__).'/lib/cmb2/init.php' ) ) include_once( 'lib/cmb2/init.php' );

Page 15: Do it in code! A guide to creating a custom site structure plugin in WordPress

15

Includes for declaring structureinclude_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');

Page 16: Do it in code! A guide to creating a custom site structure plugin in WordPress

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/

Page 17: Do it in code! A guide to creating a custom site structure plugin in WordPress

17

Thank YouFind me online:

rexrana.capeterhebert.com@RexRanaDesign@peter_hebert

peterhebertrexrana