Network: RU-Secure
Username: trsguest2017
Password: RUguest253$
Custom Meta Box
Frameworks
AN OVERVIEW AND HOW TO EXTEND THEM
Slides @ http://www.slideshare.net/pbearne
Paul Bearne @pbearne
Freelance Senior Full Stack WordPress Developer,
Core contribs in WordPress versions 3.9, 4.0, 4.2, 4.4, 4.5, 4.6, 4.7 and 4.8
Bearne.ca
Plugin author of:
Author Avatars List ( https://wordpress.org/plugins/author-avatars/ )
WP Bullhorn Job Listing ( https://wordpress.org/plugins/bh-staffing-job-listing-and-cv-upload-for-wp/ )
WP Vote (https:// wp-vote.com )
Real-time Publishing for WordPress
Livepress.com
Deep integration with Bullhorn CRM
Bullhorn Plugin
https://wordpress.org/plugins/bh-staffing-job-listing-and-cv-upload-for-wp/
#1 Outsourcing Service For WordPress
Codeable.io
$10 credit : https://codeable.io?coupon_code=WSQWI1
Custom Meta Box
Frameworks
HOW TO ADD EXTRA DATA TO A POST AND DISPLAY IT
What will we cover today?
What are meta boxes
ACF
MetaBox.io
CMB2
Field Manager
By the end, you should be able to:
Choose a solution that fits you needs
and coding level.
Create a custom MetaBox in all the
frameworks.
Why does it matter?
It allows to have fine control on how you display the data
You can control the input types
Just adding custom data is not use friendly
You can make values required
Coding meta boxes by hand is a slow and painful process
Advanced / custom input types (colour pickers, custom
data sources, etc) are complex
ACF admin gui
ACF editor gui
ACF gui
<h1><?php the_field('custom_title'); ?></h1>
<img src="<?php the_field('hero_image'); ?>" />
[acf field="{$field_name}"]
And the post to fetch from
[acf field="{$field_name}" post_id="{$post_id}"]
https://www.advancedcustomfields.com/resources/
ACF gui
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_594046ccd5949',
'title' => 'Cottage Details',
'fields' => array (
array (
'key' =>
'field_594042578f068',
'label' => 'External Photo',
'name' => 'external_photo',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'cottage’,
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
ACF in a theme or plugin
<?php
// 1. customize ACF path
add_filter('acf/settings/path', 'my_acf_settings_path’);
function my_acf_settings_path( $path ) {
$path = get_stylesheet_directory() . '/acf/’;
return $path;
}
// 2. customize ACF dir
add_filter('acf/settings/dir', 'my_acf_settings_dir’);
function my_acf_settings_dir( $dir ) {
$dir = get_stylesheet_directory_uri() . '/acf/’;
return $dir; }
// 3. Hide ACF field group menu item
add_filter('acf/settings/show_admin', '__return_false’);
// 4. Include ACF
include_once( get_stylesheet_directory() . '/acf/acf.php' );
?>
https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/
ACF in a theme or plugin
Red: Entries for config
yellow: The data with the ref
blue: data revisions !!!
ACF
PRO’S
End users can edit fields
Easy for non-coders
Lots of field types
Fully featured
Good for templating
Supported
CON’S
End users can edit fields
Stored in DB ( slows the site ) / Not in source control
Relies on custom output functions (for all types rendering)
Hard to extend: https://github.com/elliotcondon/acf-field-type-template
Need PRO version for advance stuff
ACF can output fields to PHP, eliminating the DB.
It is possible to remove ACF from wp-admin so fields won’t be changed
by users
2 rows in DB per entry
Meta Box
Free version is code only
https://github.com/rilwis/meta-box/tree/master/demo
MetaBox.io
https://metabox.io/
MetaBox.io – Online Generator
https://metabox.io/online-generator/
Meta box
PRO’S
End users can edit fields
Easy for non-coders
Lots of field types
Fully featured
Good for templating
Supported
Has easy code support
Online Generator
CON’S
End users can edit fields
May be stored in DB ( slows the site )
May not in source control
Relies on custom output functions (for all types rendering)
Hard to extend: https://github.com/elliotcondon/acf-field-type-
template
Need PRO version for advance stuff
It is possible to remove ACF from wp-admin so fields won’t be
changed by users
ACF can output fields to PHP, eliminating the DB.
https://metabox.io/docs/get-meta-value
Toolset
https://wp-types.com
CMB2
https://github.com/CMB2/CMB2
CMB2
https://github.com/CMB2/CMB2/wiki
CMB2 add input
https://github.com/CMB2/CMB2/wiki
CMB2 output
https://github.com/CMB2/CMB2/wiki
CMB2 extending
•cmb2_render_{field-type}
•cmb2_sanitize_{field-type}
function cmb2_render_callback_for_text_email( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
echo $field_type_object->input( array( 'type' => 'email' ) );
}
add_action( 'cmb2_render_text_email', 'cmb2_render_callback_for_text_email', 10, 5 );
function cmb2_sanitize_text_email_callback( $override_value, $value ) {
// not an email?
if ( ! is_email( $value ) ) {
// Empty the value
$value = ‘’;
}
return $value;
}
add_filter( 'cmb2_sanitize_text_email', 'cmb2_sanitize_text_email_callback', 10, 2 );
https://github.com/CMB2/CMB2/wiki/Adding-your-own-field-types
CMB2 extending
https://github.com/CMB2/CMB2/wiki/Adding-your-own-field-types
CMB2
PRO’S
End users can’t edit fields
In source control
Easy for non-coders
Lots of field types
Fully featured
Good support in active development
Easy to adjust the output via filters
CON’S
End users can’t edit fields
You need to create functions to display the
data
Hard to extend beyond the provided filters
Fieldmanager
http://fieldmanager.org/
Fieldmanager
http://fieldmanager.org/
Fieldmanager extending
https://github.com/alleyinteractive/wordpress-fieldmanager/wiki/Creating-New-Fieldmanager-Plugins
Fieldmanager extending
https://github.com/willgladstone/wordpress-fieldmanager-plugin/blob/master/php/class-fieldmanager-plugin.php
Fieldmanager
PRO’S
End users can’t edit fields
In source control
Easy for non-coders
Lots of field types
Fully featured
Good support
Extended via Class inheritance so you can
overload any function
WordPress VIP approved
CON’S
End users can’t edit fields
You need to create functions to display the data
You need to understand class inheritance to
extent
Not well supported / not in active development
Which one should I use?
It depends on your coding level and what you are trying to do . . .
Questions?
Hire Me...
PAUL@BEARNE.CA
http://Bearne.ca
Slides@ http://www.slideshare.net/pbearne
Email: paul@Bearne.ca

Custom post-framworks

Editor's Notes

  • #4 Some of smallest patches
  • #5 I am freelance so I need clients that will pay me to allow me to talk at WordCamp! These are some of my clients that I help with their complex problems whose products you may find useful LivePress is real time microblogging that is native on WordPress. It is available as part of WordPress.com VIP platform.
  • #6 I am freelance so I need clients that will pay me to allow me to talk at WordCamp! These are some of my clients that I help with their complex problems whose products you may find useful LivePress is real time microblogging that is native on WordPress. It is available as part of WordPress.com VIP platform.
  • #7 Uncanny Owl are eLearning experts that can help you and your clients provide eLearning solutions
  • #8 We know it's hard to find a reliable WordPress expert when you need one, which is why we've set on a mission to bring them all to one place: here. Join us and benefit from: a steady income with a guaranteed minimum hourly rate working with great, loyal clients being a part of our reputable expert community
  • #10 In this talk, I will show you how to use frameworks to create and handle custom meta boxes
  • #21 End users can edit fields: someone could change the identifier and break your implementation. It encourages a front-end dependency: the get_field( $key ) function adds some convenience with formatted fields but functionally is just a wrapper for get_post_meta( $post_id, $key, true ) and if you’ve made the mistake of buying into it you’ll have some work to do if you ever want to leave. API is developer-unfriendly: no documentation for writing field definitions by hand (using register_field_group()), the syntax for its parametised array isn’t bad but it’s far too verbose to write by guessing and every time you make a change you have to re-export the code and update your plugin. Impractical to use as a front-end dependency: you’d have to either check whether ACF is loaded before registering fields or use something like the TGM Plugin Activation library to ensure it’s available. Either way, in one way or another, you’re delegating responsibility for the dependency to the user which is one of those WordPress-specific practices that makes the wider PHP community scratch their heads. Painful to use as a back-end dependency: there isn’t a clean way to add custom fields without unintended side-effects.