// Add the Meta Box
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box', // $id
'Product details', // $title
'show_custom_meta_box', // $callback
'products', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');
// Field Array
$prefix = 'w2f_';
$custom_meta_fields = array(
array(
'label' => 'Price',
'desc' => 'Price of the product.( Use your default currency symbol)',
'id' => $prefix.'price',
'type' => 'text'
),
array(
'label' => 'On discount sale?',
'desc' => 'Check if the product is on discount sale',
'id' => $prefix.'discount',
'type' => 'checkbox'
),
array(
'label' => 'Price before discount',
'desc' => 'Price of the product before discount. Should be higher than the current price.',
'id' => $prefix.'disc-price',
'type' => 'text'
),
array(
'label' => 'Cart shortcode',
'desc' => 'copy paste the add-to-cart shortcode here ',
'id' => $prefix.'cartcode',
'type' => 'textarea'
),
array(
'label' => 'On feature',
'desc' => 'Check if the product is on featured',
'id' => $prefix.'featured',
'type' => 'checkbox'
),
);
// 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().'/js/custom-js.js');
wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/css/jquery-ui-custom.css');
}
// add some custom js to the head of the page
add_action('admin_head','add_custom_scripts');
function add_custom_scripts() {
global $custom_meta_fields, $post;
$output = '';
echo $output;
}
// The Callback
function show_custom_meta_box() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo '';
// Begin the field table and loop
echo '
';
foreach ($custom_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '
';
switch($field['type']) {
// text
case 'text':
echo '
'.$field['desc'].'';
break;
// textarea
case 'textarea':
echo '
'.$field['desc'].'';
break;
// checkbox
case 'checkbox':
echo '
';
break;
// select
case 'select':
echo ' '.$field['desc'].'';
break;
// radio
case 'radio':
foreach ( $field['options'] as $option ) {
echo '
';
}
echo ''.$field['desc'].'';
break;
// checkbox_group
case 'checkbox_group':
foreach ($field['options'] as $option) {
echo '
';
}
echo ''.$field['desc'].'';
break;
// tax_select
case 'tax_select':
echo ' Manage '.$taxonomy->label.'';
break;
// post_list
case 'post_list':
$items = get_posts( array (
'post_type' => $field['post_type'],
'posts_per_page' => -1
));
echo ' '.$field['desc'].'';
break;
// date
case 'date':
echo '
'.$field['desc'].'';
break;
// slider
case 'slider':
$value = $meta != '' ? $meta : '0';
echo '
'.$field['desc'].'';
break;
// image
case 'image':
$image = get_template_directory_uri().'/images/image.png';
echo ''.$image.'';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '
Remove Image '.$field['desc'].'';
break;
// repeatable
case 'repeatable':
echo '+
';
$i = 0;
if ($meta) {
foreach($meta as $row) {
echo '