Skip to content

Instantly share code, notes, and snippets.

@yojance
Created July 30, 2014 12:47
Show Gist options
  • Save yojance/11e8d92861f8ca12b0db to your computer and use it in GitHub Desktop.
Save yojance/11e8d92861f8ca12b0db to your computer and use it in GitHub Desktop.
OptionTree Measurement Option Type
// OptionTree Measurement Option Type
// Example code when being used as a Metabox or
// Exported OptionTree file to be used in Theme Mode
array(
'id' => 'spyr_demo_measurement',
'label' => __( 'Measurement', 'text-domain' ),
'desc' => __( 'Your description', 'text-domain' ),
'type' => 'measurement',
'section' => 'your_section',
)
// Get the value saved on Theme Options Page
// Returns an array
$spyr_demo_measurement = ot_get_option( 'spyr_demo_measurement' );
// Get the value saved for a Page, Post or CPT ( Within the loop )
// Returns an array
$spyr_demo_measurement = get_post_meta( $post->ID, 'spyr_demo_measurement', true );
// Displaying the result side by side
echo $spyr_demo_measurement[0] . $spyr_demo_measurement[1];
// Adding a new measurement option to the list
add_filter( 'ot_measurement_unit_types', 'spyr_ot_measurement_unit_types', 10, 2 );
function spyr_ot_measurement_unit_types( $measurements, $field_id ) {
if( 'demo_measurement' == $field_id ) {
return array_merge( $measurements, array( 'rem' => 'rem' ) );
}
}
// Override list of measurements
add_filter( 'ot_measurement_unit_types', 'spyr_ot_measurement_override_unit_types', 10, 2 );
function spyr_ot_measurement_override_unit_types( $measurements, $field_id ) {
if( 'demo_measurement' == $field_id ) {
return array( 'rem' => 'rem' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment