Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP repeater field
function generate_key_person_fields(newId) {
return `
<div class="key-person-container">
<div class="form-group-admin-inline">
<label>Name</label><br>
<input type="text" name="key_person[name][${newId}][]" value="" required>
</div>
<div class="form-group-admin-inline">
<label>Title</label><br>
<input type="text" name="key_person[title][${newId}][]" value="">
</div>
<div class="form-group-admin-inline">
<label>Social URL</label><br>
<input class="regular-text" type="text" name="key_person[social_url][${newId}][]" value="">
</div>
<div class="form-group-admin-inline">
<input type="checkbox" value="1" name="key_person[current_job][${newId}][]" />Current Job
</div>
<br>
<div class="form-group-admin-inline">
<input class="last-checkbox" id="${newId}" type="checkbox" value="1" name="key_person[insurance_experience][${newId}][]" />Insurance Experience
</div>
<div class="form-group-admin-inline">
<button class="remove-key-person button-inline button button-primary" type="button" name="button">Remove</button>
</div>
</div>
`;
}
$(document).on("click", ".add-key-person", function() {
let lastCheckboxId = $('.key-people-meta-box .key-person-container').last().find('.last-checkbox').attr('id') ? $('.key-people-meta-box .key-person-container').last().find('.last-checkbox').attr('id') : 'row-0';
let newId = lastCheckboxId.replace(/(\d+)+/g, function(match, number) {
return +number+1;
});
$(".key-people-meta-box").append(generate_key_person_fields(newId));
});
$(document).on("click", ".remove-key-person", function() {
$(this).closest(".key-person-container").remove();
});
<?php
function save_company_data( $post_id, $post, $update ) {
delete_post_meta( $post_id, "key_people" );
if( isset( $_POST["key_person"]["name"] ) ) {
$arr_keys = array_keys($_POST["key_person"]["name"]);
foreach ($arr_keys as $arr_key) {
$person_name = sanitize_text_field( trim( $_POST["key_person"]["name"][$arr_key][0] ) );
$title = sanitize_text_field( trim( $_POST["key_person"]["title"][$arr_key][0] ) );
$social_url = sanitize_text_field( trim( $_POST["key_person"]["social_url"][$arr_key][0] ) );
$current_job = sanitize_text_field( trim( $_POST["key_person"]["current_job"][$arr_key][0] ?? 0 ) );
$insurance_experience = sanitize_text_field( trim( $_POST["key_person"]["insurance_experience"][$arr_key][0] ?? 0 ) );
add_post_meta(
$post_id,
"key_people",
[
'name' => $person_name,
'title' => $title,
'social_url' => $social_url,
'current_job' => $current_job,
'insurance_experience' => $insurance_experience
]
);
}
}
}
add_action( 'save_post_company', 'save_company_data', 10, 3 );
function key_people_meta_box( $post ) {
$key_people = get_post_meta( $post->ID, 'key_people' );
?>
<div class="key-people-meta-box">
<div class="form-group-admin" style="margin-bottom: 20px;">
<button class="add-key-person button button-primary" type="button" name="button">Add Person</button>
</div>
<?php
if( $key_people ) {
foreach( $key_people as $index => $key_person) {
?>
<div class="key-person-container">
<div class="form-group-admin-inline">
<label>Name</label><br>
<input type="text" name="key_person[name][row-<?php echo $index; ?>][]" value="<?php echo $key_person["name"]; ?>" required>
</div>
<div class="form-group-admin-inline">
<label>Title</label><br>
<input type="text" name="key_person[title][row-<?php echo $index; ?>][]" value="<?php echo $key_person["title"]; ?>">
</div>
<div class="form-group-admin-inline">
<label>Social URL</label><br>
<input class="regular-text" type="text" name="key_person[social_url][row-<?php echo $index; ?>][]" value="<?php echo $key_person["social_url"]; ?>">
</div>
<div class="form-group-admin-inline">
<input type="checkbox" value="1" name="key_person[current_job][row-<?php echo $index; ?>][]" <?php checked( $key_person["current_job"] ?? 0, '1', TRUE ); ?> />Current Job
</div>
<br>
<div class="form-group-admin-inline">
<input class="last-checkbox" id="row-<?php echo $index; ?>" type="checkbox" value="1" name="key_person[insurance_experience][row-<?php echo $index; ?>][]" <?php checked( $key_person["insurance_experience"] ?? 0, '1', TRUE ); ?> />Insurance Experience
</div>
<div class="form-group-admin-inline">
<button class="remove-key-person button-inline button button-primary" type="button" name="button">Remove</button>
</div>
</div>
<?php
}
}
?>
</div>
<?php
}
function add_company_meta_boxes() {
add_meta_box( 'key_people', 'Key People', 'key_people_meta_box', 'company', 'normal', 'default' );
}
add_action( 'add_meta_boxes', 'add_company_meta_boxes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment