Monday 28 October 2024

Checkbox in metabox with extra field save data

 // Hook into the 'add_meta_boxes' action

add_action('add_meta_boxes', 'custom_post_type_tags_as_checkboxes', 10, 2);


function custom_post_type_tags_as_checkboxes($post_type, $post) {

    if ($post_type == 'properties') { // Change 'your_custom_post_type' to your custom post type slug

        remove_meta_box('tagsdiv-es_tag', $post_type, 'side'); // Remove the default tags metabox

        remove_meta_box('tagsdiv-es_amenity', $post_type, 'side'); // Remove the default tags metabox

        remove_meta_box('tagsdiv-es_feature', $post_type, 'side'); // Remove the default tags metabox     

remove_meta_box('tagsdiv-es_neighborhood', $post_type, 'side'); // Remove the default tags metabox

remove_meta_box('tagsdiv-es_category', $post_type, 'side'); // Remove the default tags metabox

        add_meta_box('tagsdiv-post_tag', __('Tags'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'post_tag'));

// Add custom checkboxes for Amenity taxonomy

        add_meta_box('tagsdiv-es_amenity', __('Amenity'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_amenity'));


// Add custom checkboxes for Features taxonomy

        add_meta_box('tagsdiv-es_feature', __('Feature'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_feature'));

// Add custom checkboxes for es_neighborhood taxonomy

        add_meta_box('tagsdiv-es_neighborhood', __('Neighborhoods'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_neighborhood'));


// Add custom checkboxes for es_category taxonomy

        add_meta_box('tagsdiv-es_category', __('Categories'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_category'));

    }

}


// Render checkboxes instead of the default tag input

// Render checkboxes with an option to add new terms

function render_tags_as_checkboxes($post, $box) {

    $taxonomy = $box['args']['taxonomy'];

    $terms = get_terms(array(

        'taxonomy' => $taxonomy,

        'hide_empty' => false,

    ));


    $post_terms = wp_get_post_terms($post->ID, $taxonomy, array('fields' => 'ids'));


    if (!empty($terms)) {

        echo '<div style="max-height: 150px; overflow-y: auto; border: 1px solid #ddd; padding: 5px;">';

        foreach ($terms as $term) {

            $checked = in_array($term->term_id, $post_terms) ? ' checked="checked"' : '';

            echo '<label style="display: block; margin: 3px 0;"><input type="checkbox" name="tax_input[' . $taxonomy . '][]" value="' . $term->term_id . '"' . $checked . '> ' . esc_html($term->name) . '</label>';

        }

        echo '</div>';

    }

    

    // Add new term field

    echo '<div style="margin-top: 10px;">

            <input type="text" id="new_' . $taxonomy . '_term" placeholder="Add new term" style="width: 70%;"/>

            <button type="button" class="button add-term" data-taxonomy="' . $taxonomy . '">Add</button>

          </div>';


    // JavaScript for AJAX request

    echo "<script>

        jQuery(document).ready(function($) {

            $('.add-term').click(function() {

                var taxonomy = $(this).data('taxonomy');

                var newTerm = $('#new_' + taxonomy + '_term').val();


                if (newTerm) {

                    $.ajax({

                        url: ajaxurl,

                        type: 'POST',

                        data: {

                            action: 'add_new_term',

                            taxonomy: taxonomy,

                            term_name: newTerm

                        },

                        success: function(response) {

                            if (response.success) {

                                location.reload(); // Reload to see the new term

                            } else {

                                //alert('Error adding term: ' + response.data);

                            }

                        }

                    });

                }

            });

        });

    </script>";

}



// Save the selected terms when the post is saved

add_action('save_post', 'save_custom_tag_checkboxes');


function save_custom_tag_checkboxes($post_id) {

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {

        return;

    }


    if (isset($_POST['tax_input']['post_tag'])) {

        $tag_ids = array_map('intval', $_POST['tax_input']['post_tag']);

        wp_set_post_terms($post_id, $tag_ids, 'post_tag');

    } else {

        wp_set_post_terms($post_id, array(), 'post_tag'); // Clear tags if none are selected

    }

    // Save Amenity

    if (isset($_POST['tax_input']['es_amenity'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_amenity']);

        wp_set_post_terms($post_id, $feature_ids, 'es_amenity');

    } else {

        wp_set_post_terms($post_id, array(), 'es_amenity'); // Clear features if none are selected

    }

 

// Save Features

    if (isset($_POST['tax_input']['es_feature'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_feature']);

        wp_set_post_terms($post_id, $feature_ids, 'es_feature');

    } else {

        wp_set_post_terms($post_id, array(), 'es_feature'); // Clear features if none are selected

    }

// Save es_neighborhood

    if (isset($_POST['tax_input']['es_neighborhood'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_neighborhood']);

        wp_set_post_terms($post_id, $feature_ids, 'es_neighborhood');

    } else {

        wp_set_post_terms($post_id, array(), 'es_neighborhood'); // Clear features if none are selected

    }

// Save es_neighborhood

    if (isset($_POST['tax_input']['es_category'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_category']);

        wp_set_post_terms($post_id, $feature_ids, 'es_category');

    } else {

        wp_set_post_terms($post_id, array(), 'es_category'); // Clear features if none are selected

    }

}


add_action('wp_ajax_add_new_term', 'add_new_term');


function add_new_term() {

    if (!isset($_POST['taxonomy']) || !isset($_POST['term_name'])) {

        wp_send_json_error('Invalid request');

    }


    $taxonomy = sanitize_text_field($_POST['taxonomy']);

    $term_name = sanitize_text_field($_POST['term_name']);


    // Insert the term

    $new_term = wp_insert_term($term_name, $taxonomy);


    if (is_wp_error($new_term)) {

        wp_send_json_error($new_term->get_error_message());

    } else {

        wp_send_json_success($new_term);

    }

}


No comments:

Post a Comment