diff --git a/custom-field-types/README.md b/custom-field-types/README.md new file mode 100644 index 0000000..c6b94e8 --- /dev/null +++ b/custom-field-types/README.md @@ -0,0 +1,4 @@ +Custom Field Types +========== + +Snippets for [adding custom CMB2 field types](https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-field-types). \ No newline at end of file diff --git a/custom-field-types/address-field-type.php b/custom-field-types/address-field-type.php new file mode 100644 index 0000000..d19bb0d --- /dev/null +++ b/custom-field-types/address-field-type.php @@ -0,0 +1,171 @@ + '', + 'address-2' => '', + 'city' => '', + 'state' => '', + 'zip' => '', + ) ); + + $address = '
'; + $address .= '

' . esc_html( $address['address-1'] ) . '

'; + if ( $address['address-2'] ) { + $address .= '

' . esc_html( $address['address-2'] ) . '

'; + } + $address .= '

City: ' . esc_html( $address['city'] ) . '

'; + $address .= '

State: ' . esc_html( $address['state'] ) . '

'; + $address .= '

Zip: ' . esc_html( $address['zip'] ) . '

'; + $address = '
'; + + return apply_filters( 'jt_cmb2_get_address_field', $address ); +} + +/** + * Render 'address' custom field type + * + * @since 0.1.0 + * + * @param array $field The passed in `CMB2_Field` object + * @param mixed $value The value of this field escaped. + * It defaults to `sanitize_text_field`. + * If you need the unescaped value, you can access it + * via `$field->value()` + * @param int $object_id The ID of the current object + * @param string $object_type The type of object you are working with. + * Most commonly, `post` (this applies to all post-types), + * but could also be `comment`, `user` or `options-page`. + * @param object $field_type_object The `CMB2_Types` object + */ +function jt_cmb2_render_address_field_callback( $field, $value, $object_id, $object_type, $field_type_object ) { + + // can override via the field options param + $select_text = esc_html( $field_type_object->_text( 'address_select_state_text', 'Select a State' ) ); + + $state_list = array( ''=>$select_text,'AL'=>'Alabama','AK'=>'Alaska','AZ'=>'Arizona','AR'=>'Arkansas','CA'=>'California','CO'=>'Colorado','CT'=>'Connecticut','DE'=>'Delaware','DC'=>'District Of Columbia','FL'=>'Florida','GA'=>'Georgia','HI'=>'Hawaii','ID'=>'Idaho','IL'=>'Illinois','IN'=>'Indiana','IA'=>'Iowa','KS'=>'Kansas','KY'=>'Kentucky','LA'=>'Louisiana','ME'=>'Maine','MD'=>'Maryland','MA'=>'Massachusetts','MI'=>'Michigan','MN'=>'Minnesota','MS'=>'Mississippi','MO'=>'Missouri','MT'=>'Montana','NE'=>'Nebraska','NV'=>'Nevada','NH'=>'New Hampshire','NJ'=>'New Jersey','NM'=>'New Mexico','NY'=>'New York','NC'=>'North Carolina','ND'=>'North Dakota','OH'=>'Ohio','OK'=>'Oklahoma','OR'=>'Oregon','PA'=>'Pennsylvania','RI'=>'Rhode Island','SC'=>'South Carolina','SD'=>'South Dakota','TN'=>'Tennessee','TX'=>'Texas','UT'=>'Utah','VT'=>'Vermont','VA'=>'Virginia','WA'=>'Washington','WV'=>'West Virginia','WI'=>'Wisconsin','WY'=>'Wyoming' ); + + // make sure we specify each part of the value we need. + $value = wp_parse_args( $value, array( + 'address-1' => '', + 'address-2' => '', + 'city' => '', + 'state' => '', + 'zip' => '', + ) ); + + $state_options = ''; + foreach ( $state_list as $abrev => $state ) { + $state_options .= ''; + } + + ?> +

+ input( array( + 'name' => $field_type_object->_name( '[address-1]' ), + 'id' => $field_type_object->_id( '_address_1' ), + 'value' => $value['address-1'], + ) ); ?> +
+

+ input( array( + 'name' => $field_type_object->_name( '[address-2]' ), + 'id' => $field_type_object->_id( '_address_2' ), + 'value' => $value['address-2'], + ) ); ?> +
+

+ input( array( + 'class' => 'cmb_text_small', + 'name' => $field_type_object->_name( '[city]' ), + 'id' => $field_type_object->_id( '_city' ), + 'value' => $value['city'], + ) ); ?> +
+

+ select( array( + 'name' => $field_type_object->_name( '[state]' ), + 'id' => $field_type_object->_id( '_state' ), + 'options' => $state_options, + ) ); ?> +
+

+ input( array( + 'class' => 'cmb_text_small', + 'name' => $field_type_object->_name( '[zip]' ), + 'id' => $field_type_object->_id( '_zip' ), + 'value' => $value['zip'], + 'type' => 'number', + ) ); ?> +
+ _desc( true ); + +} +add_filter( 'cmb2_render_address', 'jt_cmb2_render_address_field_callback', 10, 5 ); + + +/** + * The following snippets are required for allowing the address field + * to work as a repeatable field, or in a repeatable group + */ + +function cmb2_sanitize_address_field( $check, $meta_value, $object_id, $field_args, $sanitize_object ) { + + // if not repeatable, bail out. + if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) { + return $check; + } + + foreach ( $meta_value as $key => $val ) { + $meta_value[ $key ] = array_map( 'sanitize_text_field', $val ); + } + + return $meta_value; +} +add_filter( 'cmb2_sanitize_address', 'cmb2_sanitize_address_field', 10, 5 ); + +function cmb2_types_esc_address_field( $check, $meta_value, $field_args, $field_object ) { + // if not repeatable, bail out. + if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) { + return $check; + } + + foreach ( $meta_value as $key => $val ) { + $meta_value[ $key ] = array_map( 'esc_attr', $val ); + } + + return $meta_value; +} +add_filter( 'cmb2_types_esc_address', 'cmb2_types_esc_address_field', 10, 4 ); diff --git a/front-end/README.md b/front-end/README.md new file mode 100644 index 0000000..c864fa5 --- /dev/null +++ b/front-end/README.md @@ -0,0 +1,4 @@ +Front-end +========== + +Snippets that use CMB2 on the front-end (not wp-admin) side of your site. \ No newline at end of file diff --git a/front-end/cmb2-metabox-shortcode.php b/front-end/cmb2-metabox-shortcode.php new file mode 100644 index 0000000..a28b3c8 --- /dev/null +++ b/front-end/cmb2-metabox-shortcode.php @@ -0,0 +1,45 @@ +ID; + } + + // If no metabox id is set, yell about it + if ( empty( $atts['id'] ) ) { + return 'Please add an 'id' attribute to specify the CMB2 form to display.'; + } + + $metabox_id = esc_attr( $atts['id'] ); + $object_id = absint( $atts['post_id'] ); + // Get our form + $form = cmb2_get_metabox_form( $metabox_id, $object_id ); + + return $form; +} +add_shortcode( 'cmb-form', 'jt_cmb2_do_frontend_form_shortcode' ); diff --git a/metaboxes/README.md b/metaboxes/README.md new file mode 100644 index 0000000..31100ab --- /dev/null +++ b/metaboxes/README.md @@ -0,0 +1,4 @@ +Metaboxes +========== + +These are examples of different metabox configurations. \ No newline at end of file diff --git a/modified-field-types/README.md b/modified-field-types/README.md new file mode 100644 index 0000000..8cb9bc5 --- /dev/null +++ b/modified-field-types/README.md @@ -0,0 +1,4 @@ +Modified Field Types +========== + +These are examples of using the built-in [CMB2 field-types](https://github.com/WebDevStudios/CMB2/wiki/Field-Types), but modifiying them with the [available parameters](https://github.com/WebDevStudios/CMB2/wiki/Field-Types#common-field-parameters). \ No newline at end of file diff --git a/modified-field-types/modify-button-text-for-file-type.php b/modified-field-types/modify-button-text-for-file-type.php new file mode 100644 index 0000000..eca624c --- /dev/null +++ b/modified-field-types/modify-button-text-for-file-type.php @@ -0,0 +1,15 @@ + 'Image', + 'desc' => 'Upload an image.', + 'id' => '_jt_cmb2_image', + 'type' => 'file', + 'options' => array( + 'add_upload_file_text' => __( 'Add or Upload Image', 'jt_cmb2' ), + ), +); diff --git a/modified-field-types/readonly-field-type.php b/modified-field-types/readonly-field-type.php new file mode 100644 index 0000000..5498883 --- /dev/null +++ b/modified-field-types/readonly-field-type.php @@ -0,0 +1,15 @@ + 'Read Only', + 'description' => 'The value of this input should be saved somewhere else.', + 'id' => '_jtcmb2_readonly', + 'type' => 'text', + 'attributes' => array( + 'readonly' => 'readonly', + 'disabled' => 'disabled', + ), +); diff --git a/user-meta-and-settings/README.md b/user-meta-and-settings/README.md new file mode 100644 index 0000000..79e1b8d --- /dev/null +++ b/user-meta-and-settings/README.md @@ -0,0 +1,4 @@ +User Meta and Settings +========== + +These are examples of [using CMB2 to generate user fields](https://github.com/WebDevStudios/CMB2/wiki/Adding-metaboxes-to-user-profile). \ No newline at end of file