picture attachment in meta box
ID, ‘_defective_products’, true) ?: []; $pieces = get_post_meta($post->ID, ‘_defective_pieces’, true) ?: []; $invoice_no = get_post_meta($post->ID, ‘_defective_invoice_no’, true); $invoice_date = get_post_meta($post->ID, ‘_defective_invoice_date’, true); $notes = get_post_meta($post->ID, ‘_defective_notes’, true); $claim_amount = get_post_meta($post->ID, ‘_defective_claim_amount’, true); $pictures = get_post_meta($post->ID, ‘_defective_pictures’, true) ?: []; $cover_letter = get_post_meta($post->ID, ‘_defective_cover_letter’, true); ?>
<div class="defective-goods-meta-box">
<h4>Products & Pieces</h4>
<div id="defective-products-container">
<?php
$count = max(count($products), 1);
for ($i = 0; $i < $count; $i++):
?>
<div class="defective-product-row" style="margin-bottom:8px;">
<input type="text" name="_defective_products[]" placeholder="Product Name" value="<?php echo esc_attr($products[$i] ?? ''); ?>" style="width:45%;" />
<input type="number" name="_defective_pieces[]" placeholder="No. of Pieces" value="<?php echo esc_attr($pieces[$i] ?? ''); ?>" style="width:45%;" />
<button type="button" class="button remove-product-row">Remove</button>
</div>
<?php endfor; ?>
</div>
<button type="button" class="button" id="add-product-row">Add More</button>
<hr>
<label>Invoice No:</label>
<input type="text" name="_defective_invoice_no" value="<?php echo esc_attr($invoice_no); ?>" style="width:100%;"/><br><br>
<label>Invoice Date:</label>
<input type="date" name="_defective_invoice_date" value="<?php echo esc_attr($invoice_date); ?>" style="width:100%;"/><br><br>
<label>Notes / Comments:</label>
<textarea name="_defective_notes" rows="4" style="width:100%;"><?php echo esc_textarea($notes); ?></textarea><br><br>
<label>Pictures (Attachment):</label>
<?php if (!empty($pictures) && is_array($pictures)) : ?>
<ul>
<?php foreach ($pictures as $pic_id) : ?>
<li><?php echo wp_get_attachment_link($pic_id); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<input type="file" name="_defective_pictures[]" multiple accept="image/*"><br><br>
<label>Cover Letter (Attachment):</label>
<?php if ($cover_letter) echo wp_get_attachment_link($cover_letter); ?><br>
<input type="file" name="_defective_cover_letter"><br><br>
<label>Amount of Claim:</label>
<input type="number" step="0.01" name="_defective_claim_amount" value="<?php echo esc_attr($claim_amount); ?>" style="width:100%;"/>
</div>
<script type="text/javascript">
(function($){
$('#add-product-row').on('click', function(){
$('#defective-products-container').append('<div class="defective-product-row" style="margin-bottom:8px;"><input type="text" name="_defective_products[]" placeholder="Product Name" style="width:45%;"/><input type="number" name="_defective_pieces[]" placeholder="No. of Pieces" style="width:45%;"/><button type="button" class="button remove-product-row">Remove</button></div>');
});
$(document).on('click', '.remove-product-row', function(){
$(this).closest('.defective-product-row').remove();
});
})(jQuery);
</script>
<?php
}
// Save handler
function save_defective_goods_meta_box($post_id) {
if (!isset($_POST[‘defective_goods_meta_box_nonce’]) || !wp_verify_nonce($_POST[‘defective_goods_meta_box_nonce’], ‘defective_goods_meta_box_action’)) return;
if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) return;
if (!current_user_can(‘edit_post’, $post_id)) return;
// Products & Pieces
$products = isset($_POST['_defective_products']) ? array_map('sanitize_text_field', $_POST['_defective_products']) : [];
$pieces = isset($_POST['_defective_pieces']) ? array_map('intval', $_POST['_defective_pieces']) : [];
update_post_meta($post_id, '_defective_products', $products);
update_post_meta($post_id, '_defective_pieces', $pieces);
// Other fields
$invoice_no = sanitize_text_field($_POST['_defective_invoice_no'] ?? '');
$invoice_date = sanitize_text_field($_POST['_defective_invoice_date'] ?? '');
$notes = sanitize_textarea_field($_POST['_defective_notes'] ?? '');
$claim_amount = floatval($_POST['_defective_claim_amount'] ?? 0);
update_post_meta($post_id, '_defective_invoice_no', $invoice_no);
update_post_meta($post_id, '_defective_invoice_date', $invoice_date);
update_post_meta($post_id, '_defective_notes', $notes);
update_post_meta($post_id, '_defective_claim_amount', $claim_amount);
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
// Pictures (multiple)
if (!empty($_FILES['_defective_pictures']['name'][0])) {
$attachment_ids = [];
foreach ($_FILES['_defective_pictures']['name'] as $key => $value) {
if ($_FILES['_defective_pictures']['name'][$key]) {
$file = [
'name' => $_FILES['_defective_pictures']['name'][$key],
'type' => $_FILES['_defective_pictures']['type'][$key],
'tmp_name' => $_FILES['_defective_pictures']['tmp_name'][$key],
'error' => $_FILES['_defective_pictures']['error'][$key],
'size' => $_FILES['_defective_pictures']['size'][$key],
];
$_FILES['single_file'] = $file;
$attach_id = media_handle_upload('single_file', $post_id);
if (!is_wp_error($attach_id)) $attachment_ids[] = $attach_id;
}
}
update_post_meta($post_id, '_defective_pictures', $attachment_ids);
}
// Cover Letter
if (!empty($_FILES['_defective_cover_letter']['name'])) {
$attach_id = media_handle_upload('_defective_cover_letter', $post_id);
if (!is_wp_error($attach_id)) update_post_meta($post_id, '_defective_cover_letter', $attach_id);
}
}
add_action(‘save_post’, ‘save_defective_goods_meta_box’);