Skip to content

Commit

Permalink
Seed the snippet library w/ some snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Feb 4, 2015
1 parent 7409a30 commit ed8c4fe
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 0 deletions.
4 changes: 4 additions & 0 deletions custom-field-types/README.md
Original file line number Diff line number Diff line change
@@ -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).
171 changes: 171 additions & 0 deletions custom-field-types/address-field-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php
/*
* Plugin Name: CMB2 Custom Field Type - Address
* Description: Makes available an 'address' CMB2 Custom Field Type. Based on https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-field-types#example-4-multiple-inputs-one-field-lets-create-an-address-field
* Author: jtsternberg
* Author URI: http://dsgnwrks.pro
* Version: 0.1.0
*/

/**
* Template tag for displaying an address from the CMB2 address field type (on the front-end)
*
* @since 0.1.0
*
* @param string $metakey The 'id' of the 'address' field (the metakey for get_post_meta)
* @param integer $post_id (optional) post ID. If using in the loop, it is not necessary
*/
function jt_cmb2_address_field( $metakey, $post_id = 0 ) {
echo jt_cmb2_get_address_field( $metakey, $post_id );
}

/**
* Template tag for returning an address from the CMB2 address field type (on the front-end)
*
* @since 0.1.0
*
* @param string $metakey The 'id' of the 'address' field (the metakey for get_post_meta)
* @param integer $post_id (optional) post ID. If using in the loop, it is not necessary
*/
function jt_cmb2_get_address_field( $metakey, $post_id = 0 ) {
$post_id = $post_id ? $post_id : get_the_ID();
$address = get_post_meta( $post_id, $metakey, 1 );

// Set default values for each address key
$address = wp_parse_args( $address, array(
'address-1' => '',
'address-2' => '',
'city' => '',
'state' => '',
'zip' => '',
) );

$address = '<div class="cmb2-address">';
$address .= '<p><strongAddress:</strong> ' . esc_html( $address['address-1'] ) . '</p>';
if ( $address['address-2'] ) {
$address .= '<p>' . esc_html( $address['address-2'] ) . '</p>';
}
$address .= '<p><strong>City:</strong> ' . esc_html( $address['city'] ) . '</p>';
$address .= '<p><strong>State:</strong> ' . esc_html( $address['state'] ) . '</p>';
$address .= '<p><strong>Zip:</strong> ' . esc_html( $address['zip'] ) . '</p>';
$address = '</div><!-- .cmb2-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 .= '<option value="'. $abrev .'" '. selected( $value['state'], $abrev, false ) .'>'. $state .'</option>';
}

?>
<div><p><label for="<?php echo $field_type_object->_id( '_address_1' ); ?>"><?php echo esc_html( $field_type_object->_text( 'address_address_1_text', 'Address 1' ) ); ?></label></p>
<?php echo $field_type_object->input( array(
'name' => $field_type_object->_name( '[address-1]' ),
'id' => $field_type_object->_id( '_address_1' ),
'value' => $value['address-1'],
) ); ?>
</div>
<div><p><label for="<?php echo $field_type_object->_id( '_address_2' ); ?>'"><?php echo esc_html( $field_type_object->_text( 'address_address_2_text', 'Address 2' ) ); ?></label></p>
<?php echo $field_type_object->input( array(
'name' => $field_type_object->_name( '[address-2]' ),
'id' => $field_type_object->_id( '_address_2' ),
'value' => $value['address-2'],
) ); ?>
</div>
<div class="alignleft"><p><label for="<?php echo $field_type_object->_id( '_city' ); ?>'"><?php echo esc_html( $field_type_object->_text( 'address_city_text', 'City' ) ); ?></label></p>
<?php echo $field_type_object->input( array(
'class' => 'cmb_text_small',
'name' => $field_type_object->_name( '[city]' ),
'id' => $field_type_object->_id( '_city' ),
'value' => $value['city'],
) ); ?>
</div>
<div class="alignleft"><p><label for="<?php echo $field_type_object->_id( '_state' ); ?>'"><?php echo esc_html( $field_type_object->_text( 'address_state_text', 'State' ) ); ?></label></p>
<?php echo $field_type_object->select( array(
'name' => $field_type_object->_name( '[state]' ),
'id' => $field_type_object->_id( '_state' ),
'options' => $state_options,
) ); ?>
</div>
<div class="alignleft"><p><label for="<?php echo $field_type_object->_id( '_zip' ); ?>'"><?php echo esc_html( $field_type_object->_text( 'address_zip_text', 'Zip' ) ); ?></label></p>
<?php echo $field_type_object->input( array(
'class' => 'cmb_text_small',
'name' => $field_type_object->_name( '[zip]' ),
'id' => $field_type_object->_id( '_zip' ),
'value' => $value['zip'],
'type' => 'number',
) ); ?>
</div>
<?php
echo $field_type_object->_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 );
4 changes: 4 additions & 0 deletions front-end/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Front-end
==========

Snippets that use CMB2 on the front-end (not wp-admin) side of your site.
45 changes: 45 additions & 0 deletions front-end/cmb2-metabox-shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
* Plugin Name: CMB2 - Front-end Shortcode
* Description: Display CMB2 metabox forms on the front-end using a shortcode
* Author: jtsternberg
* Author URI: http://dsgnwrks.pro
* Version: 0.1.0
*/

/**
* Shortcode to display a CMB2 form for a post ID.
* Adding this shortcode to your WordPress editor would look something like this:
*
* [cmb-form id="test_metabox" post_id=2]
*
* The shortcode requires a metabox ID, and (optionally) can take
* a WordPress post ID (or user/comment ID) to be editing.
*
* @param array $atts Shortcode attributes
* @return string Form HTML markup
*/
function jt_cmb2_do_frontend_form_shortcode( $atts = array() ) {
global $post;

/**
* Make sure a WordPress post ID is set.
* We'll default to the current post/page
*/
if ( ! isset( $atts['post_id'] ) ) {
$atts['post_id'] = $post->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' );
4 changes: 4 additions & 0 deletions metaboxes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Metaboxes
==========

These are examples of different metabox configurations.
4 changes: 4 additions & 0 deletions modified-field-types/README.md
Original file line number Diff line number Diff line change
@@ -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).
15 changes: 15 additions & 0 deletions modified-field-types/modify-button-text-for-file-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* The 'file' type accepts any type of file which WordPress allows.
* We want to change our text to indicate that they should be images.
*/
$modified_text_file_field = array(
'name' => 'Image',
'desc' => 'Upload an image.',
'id' => '_jt_cmb2_image',
'type' => 'file',
'options' => array(
'add_upload_file_text' => __( 'Add or Upload Image', 'jt_cmb2' ),
),
);
15 changes: 15 additions & 0 deletions modified-field-types/readonly-field-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* A CMB2 Readonly Field Type
*/
$readonly = array(
'name' => 'Read Only',
'description' => 'The value of this input should be saved somewhere else.',
'id' => '_jtcmb2_readonly',
'type' => 'text',
'attributes' => array(
'readonly' => 'readonly',
'disabled' => 'disabled',
),
);
4 changes: 4 additions & 0 deletions user-meta-and-settings/README.md
Original file line number Diff line number Diff line change
@@ -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).

0 comments on commit ed8c4fe

Please sign in to comment.