Last active
July 26, 2019 15:11
-
-
Save HelloAlberuni/7fcfee1dbd5c09d8c23befdcc93c66a1 to your computer and use it in GitHub Desktop.
CMB2 Fields Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'cmb2_admin_init', 'prefixx_register_cpt_metabox' ); | |
| function prefixx_register_cpt_metabox() { | |
| /** | |
| * Metabox to be displayed on a single page ID | |
| */ | |
| $cmb_about_page = new_cmb2_box( array( | |
| 'id' => 'prefixx_about_metabox', | |
| 'title' => esc_html__( 'About Page Metabox', 'prefixx' ), | |
| 'object_types' => array( 'page' ), // Post type | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| ) ); | |
| $cmb_about_page->add_field( array( | |
| 'name' => esc_html__( 'Test Text', 'prefixx' ), | |
| 'desc' => esc_html__( 'field description (optional)', 'prefixx' ), | |
| 'id' => 'prefixx_about_text', | |
| 'type' => 'text', | |
| ) ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Text field | |
| -------------- | |
| $metabox->add_field( array( | |
| 'name' => esc_html__( 'Tooltip Text', 'cmb2' ), | |
| 'desc' => esc_html__( '', 'cmb2' ), | |
| 'id' => 'azpls_tooltip_text', | |
| 'type' => 'text', // text/text_small/text_medium/text_email | |
| ) ); | |
| URL field | |
| --------------- | |
| $metabox->add_field( array( | |
| 'name' => esc_html__( 'Link', 'cmb2' ), | |
| 'desc' => esc_html__( '', 'cmb2' ), | |
| 'id' => 'azpls_link', | |
| 'type' => 'text_url', | |
| ) ); | |
| Select field | |
| --------------- | |
| $metabox->add_field( array( | |
| 'name' => __( 'Target', 'cmb2' ), | |
| 'desc' => __( 'The target attribute specifies where to open the linked document.', 'cmb2' ), | |
| 'id' => 'azpls_target', | |
| 'type' => 'select', | |
| 'show_option_none' => true, | |
| 'default' => '_blank', | |
| 'options' => array( | |
| '_blank' => __( 'Open In New Tab', 'cmb2' ), | |
| '_self' => __( 'Open In Same Tab', 'cmb2' ), | |
| ), | |
| ) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'cmb2_admin_init', 'prefixx_register_taxonomy_metabox' ); | |
| function prefixx_register_taxonomy_metabox() { | |
| /** | |
| * Metabox to add fields to categories and tags | |
| */ | |
| $cmb_term = new_cmb2_box( array( | |
| 'id' => 'azpls_term_edit', | |
| 'title' => esc_html__( 'Category Metabox', 'prefixx' ), | |
| 'object_types' => array( 'term' ), | |
| 'taxonomies' => array( 'category', 'post_tag' ), | |
| ) ); | |
| $cmb_term->add_field( array( | |
| 'name' => esc_html__( 'Term Image', 'prefixx' ), | |
| 'desc' => esc_html__( 'field description (optional)', 'prefixx' ), | |
| 'id' => 'azpls_term_avatar', | |
| 'type' => 'file', | |
| ) ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'cmb2_admin_init', 'prefixx_register_user_profile_metabox' ); | |
| function prefixx_register_user_profile_metabox() { | |
| /** | |
| * Metabox for the user profile screen | |
| */ | |
| $cmb_user = new_cmb2_box( array( | |
| 'id' => 'azpls_user_edit', | |
| 'title' => esc_html__( 'User Profile Metabox', 'prefixx' ), | |
| 'object_types' => array( 'user' ), | |
| 'show_names' => true, | |
| 'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option. | |
| ) ); | |
| $cmb_user->add_field( array( | |
| 'name' => esc_html__( 'Facebook URL', 'prefixx' ), | |
| 'desc' => esc_html__( 'field description (optional)', 'prefixx' ), | |
| 'id' => 'azpls_user_facebookurl', | |
| 'type' => 'text_url', | |
| ) ); | |
| $cmb_user->add_field( array( | |
| 'name' => esc_html__( 'Twitter URL', 'prefixx' ), | |
| 'desc' => esc_html__( 'field description (optional)', 'prefixx' ), | |
| 'id' => 'azpls_user_twitterurl', | |
| 'type' => 'text_url', | |
| ) ); | |
| $cmb_user->add_field( array( | |
| 'name' => esc_html__( 'Google+ URL', 'prefixx' ), | |
| 'desc' => esc_html__( 'field description (optional)', 'prefixx' ), | |
| 'id' => 'azpls_user_googleplusurl', | |
| 'type' => 'text_url', | |
| ) ); | |
| $cmb_user->add_field( array( | |
| 'name' => esc_html__( 'Linkedin URL', 'prefixx' ), | |
| 'desc' => esc_html__( 'field description (optional)', 'prefixx' ), | |
| 'id' => 'azpls_user_linkedinurl', | |
| 'type' => 'text_url', | |
| ) ); | |
| $cmb_user->add_field( array( | |
| 'name' => esc_html__( 'User Field', 'prefixx' ), | |
| 'desc' => esc_html__( 'field description (optional)', 'prefixx' ), | |
| 'id' => 'azpls_user_user_text_field', | |
| 'type' => 'text', | |
| ) ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment