Skip to content

Instantly share code, notes, and snippets.

@banarsiamin
Created December 14, 2023 10:02
Show Gist options
  • Save banarsiamin/f904bbc9732a4a4c9ce197d7229f32fe to your computer and use it in GitHub Desktop.
Save banarsiamin/f904bbc9732a4a4c9ce197d7229f32fe to your computer and use it in GitHub Desktop.
Short info Meta Box repeater field for product in custom wordpress
<?php // Add meta box
function akbcustom_meta_box() {
add_meta_box(
'akbcustom_meta_box', // Unique ID
'Short info Meta Box', // Box title
'display_akbcustom_meta_box', // Callback function to display the meta box
'product', // Post type(s) to display the meta box
'normal', // Context (normal, advanced, side)
'high' // Priority (high, core, default, low)
);
}
// Display meta box content
function display_akbcustom_meta_box($post) {
// Retrieve existing repeater field data
$akbrepeater_data = get_post_meta($post->ID, '_akbcustom_repeater_field', true);
$_akb_repeater_heading = get_post_meta($post->ID, '_akb_repeater_heading', true);
?>
<style type="text/css">
#akbcustom-repeater-list{}#akbcustom-repeater-list li {
float: left;width: 100%;background: #ccccccad;padding: 4px 5px;}#akbcustom-repeater-list input {
width: 100%;}#akbcustom-repeater-list textarea {width: 100%;margin: 3px 0;}#akbcustom-repeater-list .akbremove-row {
background: red;color: #fff;border-radius: 3px;padding: 0px 7px;border: 2px solid red;cursor: pointer;}
</style>
<div class="custom-repeater-container">
<label>Repeater Field</label>
<ul id="akbcustom-repeater-list">
<?php
if ($akbrepeater_data) {
foreach ($akbrepeater_data as $key=>$row) {
$heading =isset($_akb_repeater_heading[$key])?$_akb_repeater_heading[$key]:'';
?>
<li>
<input type="text" name="akb_repeater_heading[]" value="<?php echo esc_attr($heading); ?>" />
<textarea name="akbcustom_repeater_field[]"><?php echo esc_attr($row); ?></textarea>
<span class="akbremove-row">Remove</span>
</li>
<?php
}
}
?>
</ul>
<span id="akb_add-row" class="button">Add Row</span>
</div>
<script>
jQuery(document).ready(function ($) {
$('#akb_add-row').on('click', function () {
$('#akbcustom-repeater-list').append('<li><input type="text" name="_akb_repeater_heading[]" /><textarea name="akbcustom_repeater_field[]"></textarea><span class="akbremove-row">Remove</span></li>');
});
$('#akbcustom-repeater-list').on('click', '.akbremove-row', function () {
$(this).parent().remove();
});
});
</script>
<?php
}
// Save meta box data
function save_akbcustom_meta_box($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['akbcustom_repeater_field'])) {
$akbrepeater_data = array_map('sanitize_text_field', $_POST['akbcustom_repeater_field']);
$akbrepeater_heading = array_map('sanitize_text_field', $_POST['akb_repeater_heading']);
update_post_meta($post_id, '_akbcustom_repeater_field', $akbrepeater_data);
update_post_meta($post_id, '_akb_repeater_heading', $akbrepeater_heading);
} else {
delete_post_meta($post_id, '_akbcustom_repeater_field');
delete_post_meta($post_id, '_akb_repeater_heading');
}
}
// Hook to add meta box
add_action('add_meta_boxes', 'akbcustom_meta_box');
// Hook to save meta box data
add_action('save_post', 'save_akbcustom_meta_box');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment