File: /mnt/data/dreamsrent-wp-demo/wp-content/plugins/dreamsrent-booking/inc/metabox-faq.php
<?php
// Setup action to add meta boxes and save post meta
add_action( 'load-post.php', 'rental_boxes_faq_setup' );
add_action( 'load-post-new.php', 'rental_boxes_faq_setup' );
function rental_boxes_faq_setup() {
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'dreams_rental_faq_meta_boxes' );
/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'rental_save_post_faq_class_meta', 10, 2 );
}
// Add the FAQ meta box
function dreams_rental_faq_meta_boxes() {
add_meta_box(
'rental-post-faq-class', // Unique ID
esc_html__( 'Add FAQ', 'bookingcore' ), // Title
'rental_faq_meta_box', // Callback function
'rental', // Post type where the meta box will be displayed
'normal', // Context
'default' // Priority
);
}
// Meta box content (FAQ inputs)
function rental_faq_meta_box( $post ) {
wp_nonce_field( basename( __FILE__ ), 'rental_post_faq_class_nonce' );
$post_id = get_the_ID();
?>
<div class="faq_wrapper">
<?php
// Retrieve existing FAQs (if any)
$faqs = get_post_meta( $post_id, 'car_faq', true );
if ( $faqs && is_array( $faqs ) ) {
foreach ( $faqs as $faq ) {
?>
<div class="custom-row">
<div class="col-5">
<label class="mb-0"><?php echo esc_html__('Title','bookingcore'); ?></label>
</div>
<div class="col-5">
<label class="mb-0"><?php echo esc_html__('Description','bookingcore'); ?></label>
</div>
<div class="col-2">
</div>
</div>
<div class="custom-row">
<div class="col-5">
<input type="text" name="faq_title[]" value="<?php echo esc_attr( $faq['faq_title'] ); ?>" placeholder="FAQ Title" required>
</div>
<div class="col-5">
<input type="text" name="faq_des[]" value="<?php echo esc_attr( $faq['faq_des'] ); ?>" placeholder="FAQ Description" required>
</div>
<div class="remove_location col-2">
<a href="javascript:void(0);" class="remove_button button-secondary"><?php _e( 'Remove', 'bookingcore' ); ?></a>
</div>
</div>
<?php
}
}
?>
</div>
<button class="faq_add btn fund-btn skill-add button-secondary" data-post_id="<?php echo $post_id; ?>">
<?php _e( 'Add More', 'bookingcore' ); ?>
</button>
<?php
}
// Save FAQ data
function rental_save_post_faq_class_meta( $post_id, $post ) {
// Security check
if ( !isset( $_POST['rental_post_faq_class_nonce'] ) || !wp_verify_nonce( $_POST['rental_post_faq_class_nonce'], basename( __FILE__ ) ) ) {
return $post_id;
}
// Auto-save check
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// User capability check
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// Make sure we're saving the right post type
if ( 'rental' !== $post->post_type ) {
return $post_id;
}
// Check and save FAQ fields
if ( isset( $_POST['faq_title'] ) && isset( $_POST['faq_des'] ) ) {
$faqs = [];
$faq_titles = array_map( 'sanitize_text_field', $_POST['faq_title'] );
$faq_descriptions = array_map( 'sanitize_text_field', $_POST['faq_des'] );
for ( $i = 0; $i < count( $faq_titles ); $i++ ) {
$faqs[] = array(
'faq_title' => $faq_titles[ $i ],
'faq_des' => $faq_descriptions[ $i ],
);
}
// Save FAQ array (no need to serialize)
update_post_meta( $post_id, 'car_faq', $faqs );
} else {
// Remove meta if no FAQs are provided
delete_post_meta( $post_id, 'car_faq' );
}
}
// AJAX for adding new FAQ form fields
add_action("wp_ajax_dreams_get_faq_terms", "dreams_get_faq_terms");
add_action("wp_ajax_nopriv_dreams_get_faq_terms", "dreams_get_faq_terms");
function dreams_get_faq_terms() {
if( isset($_POST['post_id']) ) {
$post_id = sanitize_text_field($_POST['post_id']);
$html = '<div class="custom-row">';
$html .= '<div class="col-5">';
$html .= '<input type="text" name="faq_title[]" placeholder="FAQ Title" required>';
$html .= '</div>';
$html .= '<div class="col-5">';
$html .= '<input type="text" name="faq_des[]" placeholder="FAQ Description" required>';
$html .= '</div>';
$html .= '<div class="remove_location col-2">
<a href="javascript:void(0);" class="remove_button button-secondary">' . __("Remove", 'bookingcore') . '</a>
</div>';
$html .= '</div>';
echo $html;
}
wp_die(); // Properly terminate the request
}