Skip to content

Instantly share code, notes, and snippets.

@WagnerMatos
WagnerMatos / custom-meta-box #1
Created January 3, 2013 19:01
Reusable custom meta box #1
// Metaboxes
require_once('library/metaboxes.php');
//// Load scripts and styles for Meta box */
// enqueue scripts and styles, but only if is_admin
if(is_admin()) {
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_script('jquery-ui-slider');
wp_enqueue_script('custom-js', get_template_directory_uri().'/library/metaboxes/js/custom-js.js');
wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/library/metaboxes/css/jquery-ui-custom.css');
}
// Wrap all categories in a function
function wcat2() {
$out = array();
$categories = get_categories();
foreach( $categories as $category ) {
$out[$category->term_id] = array(
'label' => $category->slug,
'value' => $category->term_id
);
}
// Field Array
$prefix = 'pages_';
$page_custom_meta_fields = array(
array(
'label' => 'Slider On/Off',
'desc' => 'Tick here if you would like a slider on this page (This will override the theme settings).',
'id' => $prefix.'slider_default_onoff',
'type' => 'select',
'options' => array (
'one' => array (
// The Callback
function page_show_custom_meta_box() {
global $page_custom_meta_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
foreach ($page_custom_meta_fields as $field) {
// get value of this field if it exists for this post
// Save the Data
function page_save_custom_meta($post_id) {
global $page_custom_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
$prefix = 'pages_';
'id' => $prefix.'slider_default_onoff',
$home = get_post_meta( $post->ID, 'home_slider_default_onoff', true );
$prefix = 'pages_';
'id' => $prefix.'slider_default_onoff',
if ( $home == on ) {
// do something
}