Skip to content

Instantly share code, notes, and snippets.

@insaurabh
Created August 21, 2018 09:32
Show Gist options
  • Save insaurabh/bbbf3ef17c7877421d0bd6370c4b35dd to your computer and use it in GitHub Desktop.
Save insaurabh/bbbf3ef17c7877421d0bd6370c4b35dd to your computer and use it in GitHub Desktop.
add meta box with input field repeater wordpress
<?php
/**
* Create custom meta box
*/
// code is taken from stackoverflow and did some changes as per requirements.
add_action( 'add_meta_boxes', 'dynamic_add_box' );
/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );
/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_box() {
add_meta_box(
'review_meta_box_id', // custom id unique
__( 'Questionnaire Options', 'hb_review_rating' ),
'dynamic_inner_custom_box', // calback function
'review_questionnaire'); // post name ( array )
}
/* Prints the box content */
function dynamic_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'questionnaire_noncename' );
?>
<div id="meta_inner">
<?php
//get the saved meta as an array
$question_options = get_post_meta($post->ID,'review_questionnaireOptions', true);
$c = 0;
if ( count($question_options) > 0 && !empty($question_options)) {
foreach( $question_options as $options ) {
if ( isset( $options['option'] ) || isset( $options['star'] ) ) {
printf( '<p>Option Name <input type="text" name="question_options[%1$s][option]" value="%2$s" /> -- Star number : <input type="text" name="question_options[%1$s][star]" value="%3$s" /><button class="optionsRemove dashicons-before dashicons-trash" title="%4$s"></button></p>', $c, $options['option'], $options['star'], __( 'Remove Option' ) );
$c = $c +1;
}
}
}
?>
<span id="hb_options_wrapper"></span>
<button class="optionsAdd dashicons-before dashicons-plus-alt" title="<?php _e('Add Options'); ?>"><?php _e('Add Options'); ?></button>
// move script to particular js file
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".optionsAdd").click(function() {
count = count + 1;
// i hate append will update it soon
$('#hb_options_wrapper').append('<p>Option Name <input type="text" name="question_options['+count+'][option]" value="" /> -- Star number : <input type="text" name="question_options['+count+'][star]" value="" /><button class="optionsRemove dashicons-before dashicons-trash" title="<?php _e('Add Options'); ?>"></button></p>' );
return false;
});
// using .live as per jquery current version in project
$(".optionsRemove").live('click', function() {
$(this).parent().remove();
});
});
</script>
</div><?php
}
/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !isset( $_POST['questionnaire_noncename'] ) )
return;
if ( !wp_verify_nonce( $_POST['questionnaire_noncename'], plugin_basename( __FILE__ ) ) )
return;
// OK, we're authenticated: we need to find and save the data
$question_options = $_POST['question_options'];
update_post_meta($post_id,'review_questionnaireOptions',$question_options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment