Created
July 26, 2012 13:14
-
-
Save Abban/3181992 to your computer and use it in GitHub Desktop.
WP: Custom Metabox Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#fabfolio_options .inside{ | |
margin:0; | |
} | |
#fabfolio_options .inside >div p{ | |
margin: 0 -10px; | |
padding: 10px 10px 20px; | |
border-bottom: 1px solid #ccc; | |
border-top: 1px solid #fff; | |
} | |
#fabfolio_options .inside >div p:last-child{ | |
border-bottom:0; | |
} | |
#fabfolio_options .inside >div p:first-child{ | |
border-top:0; | |
} | |
#fabfolio_options label{ | |
font-weight:bold; | |
display:block; | |
} | |
#fabfolio_options label > span{ | |
display:block; | |
font-weight: normal; | |
float: right; | |
color:#666; | |
font-style:italic; | |
} | |
#fabfolio_options input[type="text"], #fabfolio_options select{ | |
box-sizing: border-box; | |
width: 100%; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
var formfield; | |
function show_fields(val){ | |
if(val == 0 || val == 'image' || val == 'gallery'){ | |
$('#fabfolio_options').parent().hide(); | |
$('#fabfolio_options input, #fabfolio_options select').attr('disabled', 'disabled'); | |
}else{ | |
$('#fabfolio_options').parent().show(); | |
$('#fabfolio_options input, #fabfolio_options select').attr('disabled', 'disabled'); | |
$('#fabfolio_' + val + '_fields').show().siblings().hide(); | |
$('#fabfolio_' + val + '_fields input, #fabfolio_' + val + '_fields select').removeAttr("disabled"); | |
} | |
} | |
$('#post-formats-select input[type="radio"]').live('change', function(){ | |
show_fields($(this).val()); | |
}); | |
$('.add-file').live('click', function(){ | |
formfield = $('#' + $(this).attr('id') + '_field'); | |
// show the thickbox | |
tb_show( $(this).val() , fab_admin_url + 'media-upload.php?post_id=' + fab_post_id + '&type=file&acf_type=file&TB_iframe=1'); | |
return false; | |
}); | |
window.original_send_to_editor = window.send_to_editor; | |
window.send_to_editor = function(html){ | |
if(formfield){ | |
fileurl = $('img',html).attr('src'); | |
formfield.val(fileurl); | |
tb_remove(); | |
}else{ | |
window.original_send_to_editor(html); | |
} | |
}; | |
window.original_tb_remove = window.tb_remove; | |
window.tb_remove = function(){ | |
original_tb_remove(); | |
formfield = ''; | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('THEME_PREFIX', 'fabfolio'); | |
add_action('admin_head', 'custom_metabox_assets'); | |
add_action('add_meta_boxes', 'theme_custom_box'); | |
add_action('save_post', 'theme_save_postdata'); | |
add_action('admin_head-media-upload-popup', 'theme_popup_options'); | |
add_action('init', 'custom_theme_options'); | |
/** | |
* Custom Theme Options | |
* | |
* Turn on various custom options and theme supports | |
*/ | |
function custom_theme_options() { | |
// Turn on Post Formats | |
add_theme_support('post-formats', array('image', 'gallery', 'video', 'audio', 'link', 'quote')); | |
} | |
/** | |
* Custom Metabox Assets | |
* | |
* Attaches some custom js to the admin head to handle post metaboxes | |
* | |
* @since 1.0 | |
*/ | |
function custom_metabox_assets(){ | |
echo '<script type="text/javascript"> | |
var fab_admin_url = "' .admin_url() .'"; | |
var fab_post_id = "' .$post->ID .'"; | |
</script>'; | |
wp_register_script(THEME_PREFIX .'_scripts', get_template_directory_uri() ."/admin/custom-fields.js", false, '1.0'); | |
wp_enqueue_script(THEME_PREFIX .'_scripts'); | |
wp_register_style(THEME_PREFIX .'_styles', get_template_directory_uri() .'/admin/custom-fields.css'); | |
wp_enqueue_style(THEME_PREFIX .'_styles'); | |
} | |
/** | |
* FabFolio Custom Box | |
* | |
* Adds a box to the main column on the Post and Page edit screens | |
* | |
* @since 1.0 | |
*/ | |
function theme_custom_box(){ | |
add_meta_box( | |
'fabfolio_options', | |
__('FabFolio Options', THEME_PREFIX), | |
'theme_custom_fields', | |
'post' | |
); | |
} | |
/** | |
* Get Fields | |
* @return [type] [description] | |
*/ | |
function theme_get_fields($post_id){ | |
return array( | |
THEME_PREFIX .'_video_fields' => array( | |
THEME_PREFIX .'_video_type' => array( | |
'type' => 'select', | |
'id' => THEME_PREFIX .'_video_type', | |
'label' => __('Video Type', THEME_PREFIX), | |
'description' => __('The type of video you are embedding', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_video_type', true), | |
'options' => array('youtube' => 'YouTube', 'vimeo' => 'Vimeo', 'self' => 'Self Hosted'), | |
), | |
THEME_PREFIX .'_video_url' => array( | |
'type' => 'text', | |
'id' => THEME_PREFIX .'_new_field', | |
'label' => __('Video URL', THEME_PREFIX), | |
'description' => __('The YouTube or Vimeo URL', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_new_field', true), | |
), | |
THEME_PREFIX .'_video_file' => array( | |
'type' => 'file', | |
'id' => THEME_PREFIX .'_video_file', | |
'label' => __('Video File', THEME_PREFIX), | |
'description' => __('Upload your video file', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_video_file', true), | |
'button_text' => 'Upload Video', | |
) | |
), | |
THEME_PREFIX .'_audio_fields' => array( | |
THEME_PREFIX .'_audio_file' => array( | |
'type' => 'file', | |
'id' => THEME_PREFIX .'_audio_file', | |
'label' => __('Audio File', THEME_PREFIX), | |
'description' => __('Upload your audio file', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_audio_file', true), | |
'button_text' => 'Upload Audio', | |
) | |
), | |
THEME_PREFIX .'_link_fields' => array( | |
THEME_PREFIX .'_link_url' => array( | |
'type' => 'text', | |
'id' => THEME_PREFIX .'_link_url', | |
'label' => __('Link', THEME_PREFIX), | |
'description' => __('The webpage you are linking to', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_link_url', true), | |
), | |
THEME_PREFIX .'_link_text' => array( | |
'type' => 'text', | |
'id' => THEME_PREFIX .'_link_text', | |
'label' => __('Link Text', THEME_PREFIX), | |
'description' => __('The text you want displayed', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_link_text', true), | |
), | |
), | |
THEME_PREFIX .'_quote_fields' => array( | |
THEME_PREFIX .'_quote_text' => array( | |
'type' => 'text', | |
'id' => THEME_PREFIX .'_quote_text', | |
'label' => __('Quote', THEME_PREFIX), | |
'description' => __('The quote you want displayed', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_quote_text', true), | |
), | |
THEME_PREFIX .'_quote_attr' => array( | |
'type' => 'text', | |
'id' => THEME_PREFIX .'_quote_attr', | |
'label' => __('Attribution', THEME_PREFIX), | |
'description' => __('The person who said it', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_quote_attr', true), | |
), | |
THEME_PREFIX .'_quote_url' => array( | |
'type' => 'text', | |
'id' => THEME_PREFIX .'_quote_url', | |
'label' => __('Attribution Link', THEME_PREFIX), | |
'description' => __('A link to their site', THEME_PREFIX), | |
'value' => get_post_meta($post_id, THEME_PREFIX .'_quote_url', true), | |
), | |
), | |
); | |
} | |
/** | |
* Fabfolio Custom Fields | |
* | |
* Prints the content in the box | |
* | |
* @since 1.0 | |
* @param obj $post | |
*/ | |
function theme_custom_fields($post){ | |
// Use nonce for verification | |
wp_nonce_field(plugin_basename( __FILE__ ), 'fabfolio'); | |
$format = THEME_PREFIX .'_' .get_post_format($post->ID) .'_fields'; | |
// Set up the fields | |
$custom_fields_printout = theme_get_fields($post->ID); | |
foreach($custom_fields_printout as $section => $fields){ | |
echo '<div id="' .$section .'"'; | |
if($format != $section) echo ' style="display:none;"'; | |
echo '>'; | |
foreach($fields as $name => $field){ | |
$disabled = ($format != $section) ? ' disabled="disabled"' : ''; | |
$field_function = 'theme_build_' .$field['type']; | |
echo '<p>'; | |
if(function_exists($field_function)) $field_function($name, $field, $disabled); | |
echo '</p>'; | |
} | |
echo '</div>'; | |
} | |
} | |
/** | |
* FabFolio Save Postdta | |
* | |
* Catches a post save and saves the metabox data | |
* | |
* @since 1.0 | |
* @param int $post_id | |
*/ | |
function theme_save_postdata($post_id){ | |
// Don't save anything if it's an autosave | |
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; | |
// Verify the nonce to make sure post isn't being saved from post list page | |
if(!wp_verify_nonce($_POST['fabfolio'], plugin_basename(__FILE__))) return; | |
// Check current users permissions | |
if('page' == $_POST['post_type']){ | |
if(!current_user_can('edit_page', $post_id)) return; | |
}else{ | |
if(!current_user_can('edit_post', $post_id)) return; | |
} | |
// Set up the fields | |
$custom_fields_printout = get_fields($post_id); | |
// loop through fields and save the data | |
foreach($custom_fields_printout as $fields){ | |
foreach($fields as $name => $field){ | |
$old = $field['value']; | |
$new = $_POST[$name]; | |
if($new && $new != $old){ | |
update_post_meta($post_id, $name, $new); | |
}elseif($old && $new == ''){ | |
delete_post_meta($post_id, $name, $old); | |
} | |
} | |
} | |
} | |
/** | |
* Echo Label | |
* | |
* @since 1.0 | |
* @param boolean $input | |
* @return string | |
*/ | |
function theme_echo_label($input = false){ | |
if(!$input) return false; | |
echo '<label for="' .$input['id'] .'">' .$input['label'] .':<span>' .$input['description'] .'.</span></label>'; | |
} | |
/** | |
* Build Text | |
* | |
* @since 1.0 | |
* @param boolean $name | |
* @param boolean $input | |
* @param boolean $disabled | |
* @return string | |
*/ | |
function theme_build_text($name = false, $input = false, $disabled = ''){ | |
if(!$name || !$input) return false; | |
theme_echo_label($input); | |
echo '<input' .$disabled .' type="' .$input['type'] .'" id="' .$input['id'] .'" name="' .$name .'" value="' .$input['value'] .'" >'; | |
} | |
/** | |
* Build Select | |
* | |
* @since 1.0 | |
* @param boolean $name | |
* @param boolean $input | |
* @param boolean $disabled | |
* @return string | |
*/ | |
function theme_build_select($name = false, $input = false, $disabled = false){ | |
if(!$name || !$input) return false; | |
theme_echo_label($input); | |
echo '<select' .$disabled .' name="' .$name .'" id="' .$input['id'] .'">'; | |
foreach($input['options'] as $value => $text){ | |
echo '<option'; | |
if($value == $input['value']) echo ' selected="selected"'; | |
echo ' value="' .$value .'">' .$text .'</option>'; | |
} | |
echo '</select>'; | |
} | |
/** | |
* Build File | |
* | |
* @since 1.0 | |
* @param boolean $name | |
* @param boolean $input | |
* @param boolean $disabled | |
* @return string | |
*/ | |
function theme_build_file($name = false, $input = false, $disabled = false){ | |
if(!$name || !$input) return false; | |
theme_echo_label($input); | |
echo '<input' .$disabled .' class="value" class="file-input" id="' .$input['id'] .'_field" type="hidden" name="' .$name .'" value="' .$input['value'] .'" />'; | |
echo '<span class="file_uploaded"><span class="file_name">' .$input['value'] .'</span>'; | |
echo '<a href="#" class="remove_file">' .__('Remove','fabfolio') .'</a></span>'; | |
echo '<span class="file_empty"><input' .$disabled .' type="button" id="' .$input['id'] .'" class="button add-file" value="Add File">' .'</span>'; | |
} | |
function theme_popup_options(){ | |
// defults | |
$access = false; | |
$tab = "type"; | |
// GET | |
if(isset($_GET["acf_type"]) && $_GET['acf_type'] == 'file'){ | |
$access = true; | |
if(isset($_GET['tab'])) $tab = $_GET['tab']; | |
if(isset($_POST["attachments"])){ | |
echo '<div class="updated"><p>' . __("Media attachment updated.") . '</p></div>'; | |
} | |
} | |
if($access){ | |
?><style type="text/css"> | |
#media-upload-header #sidemenu li#tab-type_url, | |
#media-upload-header #sidemenu li#tab-gallery, | |
#media-items .media-item a.toggle, | |
#media-items .media-item tr.image-size, | |
#media-items .media-item tr.align, | |
#media-items .media-item tr.url, | |
#media-items .media-item .slidetoggle { | |
display: none !important; | |
} | |
#media-items .media-item { | |
min-height: 68px; | |
} | |
#media-items .media-item .fab-checkbox { | |
float: left; | |
margin: 28px 10px 0; | |
} | |
#media-items .media-item .pinkynail { | |
max-width: 64px; | |
max-height: 64px; | |
display: block !important; | |
} | |
#media-items .media-item .filename.new { | |
min-height: 0; | |
padding: 20px 10px 10px 10px; | |
line-height: 15px; | |
} | |
#media-items .media-item .title { | |
line-height: 14px; | |
} | |
#media-items .media-item .fab-select { | |
float: right; | |
margin: 22px 12px 0 10px; | |
} | |
#media-upload .ml-submit { | |
display: none !important; | |
} | |
#media-upload .fab-submit { | |
margin: 1em 0; | |
padding: 1em 0; | |
position: relative; | |
overflow: hidden; | |
display: none; | |
} | |
#media-upload .fab-submit a { | |
float: left; | |
margin: 0 10px 0 0; | |
} | |
</style> | |
<script type="text/javascript"> | |
(function($){ | |
// Add buttons to media items | |
function fab_add_buttons() | |
{ | |
// add buttons to media items | |
$('#media-items .media-item').each(function(){ | |
if($(this).find('.fab-select').length == 0){ | |
// needs attachment ID | |
if($(this).children('input[id*="type-of-"]').length == 0){ return false; } | |
// find id | |
var id = $(this).children('input[id*="type-of-"]').attr('id').replace('type-of-', ''); | |
// Add select button | |
$(this).find('.filename.new').before('<input type="submit" name="send[' + id + ']" id="send[' + id + ']" class="button fab-select" value="<?php _e("Select File",'fabfolio'); ?>">'); | |
} | |
}); | |
} | |
<?php if($tab == 'type'): ?> | |
var fabInterval = setInterval(function(){ | |
fab_add_buttons(); | |
}, 500); | |
<?php endif; ?> | |
// add acf input filters to allow for tab navigation | |
$(document).ready(function(){ | |
setTimeout(function(){ | |
fab_add_buttons(); | |
}, 1); | |
}); | |
})(jQuery); | |
</script><?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment