Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
2.1 kB
0
Indexable
Never
function custom_enqueue_sortable_scripts() {
    // Enqueue jQuery UI sortable
    wp_enqueue_script('jquery-ui-sortable');
    // Enqueue custom script
    wp_enqueue_script('custom-sortable-script', get_template_directory_uri() . '/js/custom-sortable.js', array('jquery', 'jquery-ui-sortable'), null, true);

    wp_localize_script('custom-sortable-script', 'customSortable', array(
        'ajax_url' => admin_url('admin-ajax.php')
    ));
}

add_action('admin_enqueue_scripts', 'custom_enqueue_sortable_scripts');

function custom_sortable_js() {
    ?>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
        $('#the-list').sortable({
            items: 'tr',
            cursor: 'move',
            axis: 'y',
            handle: '.column-title',
            update: function(event, ui) {
                var order = $(this).sortable('serialize');
                $.post(customSortable.ajax_url, {
                    action: 'save_post_order',
                    order: order
                });
            }
        });
    });
    </script>
    <?php
}
add_action('admin_footer', 'custom_sortable_js');

function save_post_order() {
    if (!is_admin()) {
        return;
    }

    if (isset($_POST['order'])) {
        $order = explode('&', $_POST['order']);
        foreach ($order as $index => $value) {
            $post_id = str_replace('post[]=', '', $value);
            wp_update_post(array(
                'ID' => intval($post_id),
                'menu_order' => $index
            ));
        }
    }
    wp_die();
}

add_action('wp_ajax_save_post_order', 'save_post_order');
add_filter('b2bking_group_post_type_supports', function($val){
	return array('title','page-attributes');
}, 10, 1);
add_filter('pre_get_posts', 'sort_backend_drag_drop_menu_order');
function sort_backend_drag_drop_menu_order($query) {
  if($query->is_admin) {

        if ($query->get('post_type') == 'b2bking_group')
        {
          $query->set('orderby', 'menu_order');
          $query->set('order', 'ASC');
        }
  }
  return $query;
}
Leave a Comment