insert term array with wp_insert_term

Question

I want to import one or more categories using the input checkbox. But I do not know how to compare the array with the checkbox.
When I use only wp_insert_term after sending the form and enter the values manually, no category is added.
My initial code is below.

<?php
if (isset($_POST['product_send']) && !empty($_POST['product_send']) && $_POST['product_send'] == 'send') {
    $taxonomy = 'product_cat';
    $related_terms = array(
        'tv' => array('samsung', 'lg', 'xvision'),
        'cellphone' => array('apple', 'samsung', 'xiaomi'),
        'monitor' => array('samsung', 'lg', 'hp'),
        'laptop' => array('hp', 'sony', 'lg'),
    );
    foreach ($related_terms as $key => $term) {
        wp_insert_term($key, $taxonomy);
        $parent_term = term_exists($key, $taxonomy);
        $term_id = $parent_term['term_id'];
        foreach ($term as $term_value) {
            wp_insert_term($term_value, $taxonomy, array(
                    'parent' => $term_id
                )
            );
        }
    }
}
function product_menu()
{
    add_submenu_page('edit.php?post_type=myproduct', 'import product', 'import product', 'administrator', 'my_product', 'product_page');
}
add_action('admin_menu', 'product_menu');
function product_page()
{
    ?>
    <div id="wpbody" role="main">
        <div id="wpbody-content">
            <div class="wrap">
                <form method="post" action="">
                    <label><input type="checkbox" name="product[]" value="tv">tv</label>
                    <label><input type="checkbox" name="product[]" value="cellphone">cellphone</label>
                    <label><input type="checkbox" name="product[]" value="laptop">laptop</label>
                    <input type="hidden" name="product_send" value="send">
                    <input type="submit" class="button-primary" value="send">
                </form>
            </div>
        </div>
    </div>
    <?php
}?>

0
ali 3 days 2021-06-10T16:06:40-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse