Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active August 29, 2015 14:01
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/e8d79fbd23deac1bd37e to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/e8d79fbd23deac1bd37e to your computer and use it in GitHub Desktop.
Modifying meta box fields in a child theme
<?php
/**
* Registering meta boxes
*
* Based on these metaboxes and a global metabox variable, but can be modified to suit others:
* @link http://www.deluxeblogtips.com/meta-box/docs/define-meta-boxes
*/
// Add new metaboxes - hook into after the metaboxes have been defined, but before they have been registered
add_action( 'admin_init', 'jdn_modify_metabox_fields', 1 );
/**
* Modify exist metaboxes to include a new text field
* @author Joshua David Nelson, josh@joshuadnelson.com
**/
function jrn_modify_metabox_fields() {
$prefix = 'prefix_';
global $meta_boxes;// or whatever variable/class, etcetera is housing your metaboxes
if( !is_null( jdn_find_in_array( $meta_boxes, 'field_id', 'field_term' ) ) ) {
$key = jdn_find_in_array( $meta_boxes, 'field_id', 'field_term' );
$fields = $meta_boxes[ $key ][ 'fields' ];
$new_field = array(
array(
'name' => 'Custom Price',
'desc' => '',
'id' => $prefix . 'custom_price',
'type' => 'text',
'clone' => false,
'std' => '',
)
);
$meta_boxes[ $key ][ 'fields' ] = array_merge( $fields, $new_field );
}
}
/**
* 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