strawberry custom fields forever - wordpress metadata and custom fields

38
Strawberry Custom Fields Forever Matt Banks @mattbanks

Upload: matt-banks

Post on 17-May-2015

767 views

Category:

Technology


7 download

DESCRIPTION

Learn how to leverage the power of WordPress custom meta boxes and fields to better organize your site, make it easier to update content, and manage your data more efficiently. We’ll cover how to set everything up in code using native WordPress functions, plugins that will speed up the process, as well as ways to display all the data on your website. We’ll also touch on the new Metadata API being developed as a feature plugin in WordPress Core to see what’s coming in the future.

TRANSCRIPT

Page 1: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Strawberry Custom Fields Forever

!Matt Banks

!@mattbanks

Page 2: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

photo credit: Sheri Bigelow, @designsimply

talk title credit: Damon Cook, @dcook

Page 3: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

What are Custom Fields and Meta boxes?

• Custom Fields allow you to attach additional information (metadata) to a post, page, custom post type, etc

• http://codex.wordpress.org/Custom_Fields

• Meta boxes are for separating and grouping your Custom Fields into logical panels in the post/page editor screen

Page 4: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Fields

Page 5: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Meta boxes

Page 6: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Adding Custom Fields and Meta boxes using Core functions

Add the Meta Box

add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args);

Page 7: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Adding Custom Fields and Meta boxes using Core functions

Create a Nonce and render the Meta Box via callback function

wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce');

Page 8: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Adding Custom Fields and Meta boxes using Core functions

Verify the nonce, sanitize the data and update the Post Meta

sanitize_text_field( $_POST['myplugin_new_field'] );

update_post_meta( $post_id, '_my_meta_value_key', $my_data);

Page 9: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Displaying Custom Field data in your Theme

get_post_meta( $post_id, $key, $single);

$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );

http://codex.wordpress.org/Function_Reference/get_post_meta

Page 10: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Playing Nice with theNew REST API

• Protected meta (prefixed with _) needs authorization to be accessed by the API. Gets hooked to ‘init’ or ‘plugins_loaded’ !register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ); !register_meta( 'post', '_my_meta_key', '', '__return_true' );

Page 11: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Adding Custom Fields and Meta boxes using Core functions

Demo

Page 12: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Adding Custom Fields and Meta boxes using Core functions

Full code examples in Codex:

http://codex.wordpress.org/Function_Reference/add_meta_box#Procedural

http://codex.wordpress.org/Function_Reference/add_meta_box#Class

Page 13: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Is there something easier?Yes!

photo credit: Sheri Bigelow

Page 14: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

With a Little Help From My Friends !

Plugins to create Custom Fields and Meta boxes

• Advanced Custom Fields (ACF)

• Custom Field Suite (CFS)

• Pods

• Custom Metaboxes and Fields

Page 15: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Advanced Custom Fields (ACF)

http://www.advancedcustomfields.com/

Page 16: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Advanced Custom Fields (ACF)

• Output data into your theme using: the_field( 'your_field_name' );

Page 17: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Advanced Custom Fields (ACF)

• Multiple field types

• Easy to use

• Premium add-ons (changing in version 5)

Page 18: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Field Suite (CFS)https://uproot.us

Page 19: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Field Suite (CFS)• Output data into your theme using:

!

$cfs->get( $field_name, $post_id, $options

);

Page 20: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Field Suite (CFS)• Includes loop field types in free version

• Easy to use

• Great documentation

Page 21: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Field Suite (CFS)

Demo

Page 22: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Podshttp://pods.io

Page 23: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Pods• Output data multiple ways into your theme:

!

pods_field_display( $pod, $id, $name, $single); !

pods_image( $image, $size, $default, $attributes, $force );

• Custom Pods loops, templates, pages

Page 24: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Pods• Extremely powerful!

• Creates Custom Post Types and Taxonomies as well as Custom Fields

• Able to extend Users, Comments, etc

• More complex than other plugins/solutions

• No loop fields (coming in v3.0)

• No meta box management (coming in v3.0)

Page 25: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Metaboxes and Fields

https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress

Page 26: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Metaboxes and Fields

• Framework to create meta boxes and fields in code

• Can be embedded in custom plugins

• Uses native WordPress functions for display

get_post_meta( $post_id, $key, $single );

Page 27: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Metaboxes and Fields

• Extensive list of available post types

• Active development

• Extendible

Page 28: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Custom Metaboxes and Fields

Demo

Page 29: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

So, what’s the best choice?

Page 30: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Well, it Depends…

• Plugins like ACF, CFS and Pods are the easiest solution for adding data for most sites

• Custom Metaboxes and Fields and Core functions are better for custom plugins, building web apps with WordPress

Page 31: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Can we help Core?

Page 32: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Metadata UI APIBeing built as a Feature Plugin

Page 33: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Metadata UI API• A work in progress

• API to add fields and forms (meta boxes) to posts, pages, custom post types. In the future, plans to add forms and fields to options, users, comments, taxonomy terms, media, etc.

• Any plugin using forms will be able to interface with the API to display them, both on the back-end and public forms

• Make Core easier for creating custom fields and meta boxes for developers

Page 34: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Metadata UI API Example

Register new Form (meta box). User, Option Group, Comment Forms planned for later.

!

register_post_form( $form_name, $post_type, $form_args );

Page 35: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Metadata UI API Example

Add Fields to Post Type. Fields can be inside a Form (meta box) or in the editor at any point.

!

register_post_field( $field_name, $post_type, $field_args );

Page 36: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Metadata UI API

• UI/UX in progress for displaying custom fields and meta boxes on post type edit screens

• Plugin authors will be able to extend field types, HTML templates, form types, etc.

Page 37: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Come Together• You can help!

• Follow along with the development on Githubhttps://github.com/wordpress-metadata/metadata-ui-api

• Weekly meetings on IRC in#wordpress-core-plugins Friday’s at 2pm ET

Page 38: Strawberry Custom Fields Forever - WordPress Metadata and Custom Fields

Hello, Goodbye !

Thank You!

If you have any questions, let me know! !

[email protected] !

Twitter: @mattbanks !

Skype: mattbanks-14 !!

https://github.com/mattbanks/strawberry-custom-fields-forever