Home / Admin / Remove Location Taxonomy
Duplicate Snippet

Embed Snippet on Your Site

Remove Location Taxonomy

Code Preview
php
<?php
// Add custom Bulk Action to the Pages view
function custom_add_bulk_action() {
    global $post_type;
    
    if ($post_type == 'page') {
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery('<option>').val('remove_all_terms').text('Remove Location Taxonomy').appendTo('select[name="action"]');
                jQuery('<option>').val('remove_all_terms').text('Remove Location Taxonomy').appendTo('select[name="action2"]');
            });
        </script>
        <?php
    }
}
add_action('admin_footer', 'custom_add_bulk_action');
// Handle custom Bulk Action
function custom_handle_bulk_action($redirect_to, $action, $post_ids) {
    if ($action == 'remove_all_terms' && !empty($post_ids)) {
        foreach ($post_ids as $post_id) {
            
                wp_set_post_terms($post_id, array(), 'location_category');
        }
        $redirect_to = add_query_arg('bulk_terms_removed', count($post_ids), $redirect_to);
    }
    return $redirect_to;
}
add_filter('handle_bulk_actions-edit-page', 'custom_handle_bulk_action', 10, 3);
// Display a notice after removing terms
function custom_bulk_admin_notices() {
    if (isset($_GET['bulk_terms_removed']) && intval($_GET['bulk_terms_removed']) > 0) {
        $removed_count = intval($_GET['bulk_terms_removed']);
        printf(
            '<div id="message" class="updated fade"><p>%s %s.</p></div>',
            esc_html($removed_count),
            _n('term has been removed from the selected page.', 'terms have been removed from the selected pages.', $removed_count)
        );
    }
}
add_action('admin_notices', 'custom_bulk_admin_notices');

Comments

Add a Comment