Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuadavidnelson/993b17787149a8da775d to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/993b17787149a8da775d to your computer and use it in GitHub Desktop.
Modify medtaboxes
<?php
/**
* Registering meta boxes, based on these metaboxes:
* @link http://www.deluxeblogtips.com/meta-box/docs/define-meta-boxes
*/
// Add new metaboxes
add_action( 'admin_init', 'jdn_modify_metabox_fields', 1 );
/**
* Modify exist metaboxes to include a new field
* @author Joshua David Nelson, josh@joshuadnelson.com
* @since 1.0.0
**/
function jdn_modify_metabox_fields() {
$prefix = 'nt_';
global $meta_boxes;
if( !is_null( jdn_find_in_array( $meta_boxes, 'propertyprice', 'id' ) ) ) {
$key = jdn_find_in_array( $meta_boxes, 'propertyprice', 'id' );
$fields = $meta_boxes[ $key ][ 'fields' ];
$new_field = array(
array(
'name' => 'Custom Price',
'desc' => 'This will override the pricing options above.',
'id' => $prefix . 'custom_price',
'type' => 'text',
'clone' => false,
'std' => '',
)
);
$meta_boxes[ $key ][ 'fields' ] = array_merge( $fields, $new_field );
// Replace a Field values
$sub_key = jdn_find_in_array( $meta_boxes[ $key ][ 'fields' ], 'nt_status', 'id' );
$meta_boxes[ $key ][ 'fields' ][ $sub_key ][ 'options' ] = array(
'for-sale' => 'for Sale',
'for-lease' => 'for Lease', // keeping slug to avoid errors elsewhere
'for-sublease' => 'for Sublease',
'sold' => 'Sold',
'upcoming' => 'Upcoming',
'inquire' => 'Inquire'
);
}
}
/**
* Function to search for a metabox field and return the key
* @author Joshua David Nelson, josh@joshuadnelson.com
* @link http://stackoverflow.com/questions/8102221/php-multidimensional-array-searching-find-key-by-specific-value
**/
function jdn_find_in_array( $array, $needle, $term ) {
foreach( $array as $key => $value ) {
if ( $value[ $term ] === $needle )
return $key;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment