Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active August 23, 2024 12:14
Show Gist options
  • Save Qubadi/d819aa3e7d3d01b32441be9af3a01d48 to your computer and use it in GitHub Desktop.
Save Qubadi/d819aa3e7d3d01b32441be9af3a01d48 to your computer and use it in GitHub Desktop.
NEW UPDATE: JetEngine Listing Grid: Custom Post Types, Drag-and-Drop Functionality on the Backend Dynamicly
Youtube Tutorials:
https://www.youtube.com/watch?v=-wezi647qos&ab_channel=HeskeAgencydev
________________________________________________________
// Add Dashboard Widget for Custom Post Types
add_action('wp_dashboard_setup', 'custom_cpt_dashboard_widget');
function custom_cpt_dashboard_widget() {
wp_add_dashboard_widget('custom_cpt_dashboard_widget', 'Manage Custom Post Types', 'custom_cpt_dashboard_widget_display');
}
// Display Dashboard Widget Form
function custom_cpt_dashboard_widget_display() {
$cpt_data = get_option('custom_cpt_data', []);
// Handle form submission for adding/editing CPTs
if (isset($_POST['submit_cpt']) && check_admin_referer('cpt_nonce')) {
$index = isset($_POST['index']) ? intval($_POST['index']) : -1;
$title = sanitize_text_field($_POST['cpt_title']);
$name = sanitize_text_field($_POST['cpt_name']);
$cpt_info = ['title' => $title, 'name' => $name];
if ($index >= 0 && isset($cpt_data[$index])) {
$cpt_data[$index] = $cpt_info;
} else {
array_unshift($cpt_data, $cpt_info);
}
update_option('custom_cpt_data', $cpt_data);
}
// Handle remove requests
if (isset($_POST['remove_cpt']) && check_admin_referer('cpt_nonce')) {
$index = isset($_POST['index']) ? intval($_POST['index']) : -1;
if ($index >= 0 && isset($cpt_data[$index])) {
array_splice($cpt_data, $index, 1);
update_option('custom_cpt_data', $cpt_data);
}
}
// Display form for adding a new CPT or editing an existing one
$edit_index = -1;
if (isset($_POST['edit_cpt'])) {
$edit_index = isset($_POST['index']) ? intval($_POST['index']) : -1;
}
$current_title = $edit_index >= 0 ? $cpt_data[$edit_index]['title'] : '';
$current_name = $edit_index >= 0 ? $cpt_data[$edit_index]['name'] : '';
custom_cpt_form($current_title, $current_name, $edit_index);
// Display existing CPTs
echo '<h4>Existing Custom Post Types:</h4>';
echo '<ul style="list-style: none; padding: 0;">';
foreach ($cpt_data as $index => $cpt) {
echo '<li style="margin-bottom: 10px; padding: 10px; border: 1px solid #ddd; border-radius: 6px;">';
echo 'Title: ' . esc_html($cpt['title']) . ', Name: ' . esc_html($cpt['name']) . '<br>';
echo '<form method="post" style="display:inline; margin-left: 0px;"><input type="hidden" name="index" value="' . esc_attr($index) . '">';
echo '<input type="submit" name="edit_cpt" value="Edit" style="margin-right: 5px; background-color: #0073aa; color: white; border: none; border-radius: 6px; padding: 5px 10px; margin-top: 10px;">';
echo '<input type="submit" name="remove_cpt" value="Remove" style="background-color: #dc3232; color: white; border: none; border-radius: 6px; padding: 5px 10px; margin-top: 10px;">';
wp_nonce_field('cpt_nonce');
echo '</form>';
echo '</li>';
}
echo '</ul>';
}
// Form for adding/editing a CPT
function custom_cpt_form($title = '', $name = '', $index = -1) {
?>
<form action="" method="post" style="margin-bottom: 20px;">
<input type="hidden" name="index" value="<?php echo esc_attr($index); ?>">
<p>
<label for="cpt_title">Title:</label>
<input type="text" id="cpt_title" name="cpt_title" value="<?php echo esc_attr($title); ?>" style="width: 100%; padding: 5px; border-radius: 6px; border: 1px solid #ddd;">
</p>
<p>
<label for="cpt_name">Name (slug):</label>
<input type="text" id="cpt_name" name="cpt_name" value="<?php echo esc_attr($name); ?>" style="width: 100%; padding: 5px; border-radius: 6px; border: 1px solid #ddd;">
</p>
<?php wp_nonce_field('cpt_nonce'); ?>
<input type="submit" name="submit_cpt" value="<?php echo $index >= 0 ? 'Save Changes' : 'Add CPT'; ?>" style="background-color: #0073aa; color: white; border: none; border-radius: 6px; padding: 5px 10px;">
</form>
<?php
}
// Register CPTs dynamically based on saved option
add_action('init', 'register_custom_cpts');
function register_custom_cpts() {
$cpt_data = get_option('custom_cpt_data', []);
foreach ($cpt_data as $cpt) {
register_post_type($cpt['name'], array(
'labels' => array('name' => $cpt['title']),
'public' => true,
// ... other arguments for register_post_type()
));
}
}
// Enqueue scripts and styles for the dynamically registered CPTs
add_action('admin_enqueue_scripts', 'enqueue_cpt_sortable_script_and_style');
function enqueue_cpt_sortable_script_and_style($hook) {
global $typenow;
$cpt_data = get_option('custom_cpt_data', []);
$cpt_names = array_column($cpt_data, 'name');
if (in_array($typenow, $cpt_names)) {
wp_enqueue_script('jquery-ui-sortable');
wp_localize_script('jquery-ui-sortable', 'cpt_sortable', array(
'nonce' => wp_create_nonce('cpt_sortable_nonce')
));
echo '<style>#the-list tr td {cursor: move;} .column-numbering {width: 50px;}</style>';
}
}
// Add a new column for numbering dynamically registered CPTs
add_filter('manage_posts_columns', 'add_cpt_numbering_column');
function add_cpt_numbering_column($columns) {
global $typenow;
$cpt_data = get_option('custom_cpt_data', []);
$cpt_names = array_column($cpt_data, 'name');
if (in_array($typenow, $cpt_names)) {
$num_columns = array_slice($columns, 0, 1, true) + ['numbering' => 'No.'] + array_slice($columns, 1, NULL, true);
return $num_columns;
}
return $columns;
}
// Populate the new column for dynamically registered CPTs
add_action('manage_posts_custom_column', 'populate_cpt_numbering_column', 10, 2);
function populate_cpt_numbering_column($column, $post_id) {
static $row_number = 0;
if ('numbering' == $column) {
echo ++$row_number;
}
}
// Reset row number on each new page/load for dynamically registered CPTs
add_filter('views_edit-post', 'reset_cpt_row_number');
function reset_cpt_row_number($views) {
global $typenow;
$cpt_data = get_option('custom_cpt_data', []);
$cpt_names = array_column($cpt_data, 'name');
if (in_array($typenow, $cpt_names)) {
remove_action('manage_posts_custom_column', 'populate_cpt_numbering_column', 10);
add_action('manage_posts_custom_column', 'populate_cpt_numbering_column', 10, 2);
add_action('admin_head', 'initialize_row_number');
}
return $views;
}
function initialize_row_number() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var rowNumber = 1;
$('#the-list tr').each(function() {
$(this).find('td.column-numbering').html(rowNumber++);
});
});
</script>
<?php
}
// Update the JavaScript for drag-and-drop and add renumbering functionality for dynamically registered CPTs
add_action('admin_footer', 'cpt_sortable_and_renumber_script');
function cpt_sortable_and_renumber_script() {
global $typenow;
$cpt_data = get_option('custom_cpt_data', []);
$cpt_names = array_column($cpt_data, 'name');
if (in_array($typenow, $cpt_names)) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var sortList = $('#the-list'); // Get the list
sortList.sortable({
update: function(event, ui) {
$.ajax({
url: ajaxurl,
type: 'POST',
dataType: 'json',
data: {
action: 'save_sort_cpt',
order: sortList.sortable('toArray').toString(),
nonce: cpt_sortable.nonce
},
success: function() {
renumberRows();
}
});
},
stop: function(event, ui) {
renumberRows();
}
});
// Function to renumber rows
function renumberRows() {
$('#the-list tr').each(function(index) {
$(this).find('td.column-numbering').html(index + 1);
});
}
});
</script>
<?php
}
}
// Handle AJAX request to save order for dynamically registered CPTs
add_action('wp_ajax_save_sort_cpt', 'save_sort_cpt');
function save_sort_cpt() {
// Verify nonce
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'cpt_sortable_nonce')) {
wp_die('Nonce verification failed');
}
global $wpdb;
$order = explode(',', $_POST['order']);
foreach ($order as $index => $post_id) {
$post_id = str_replace('post-', '', $post_id); // Sanitize post ID
$wpdb->update($wpdb->posts, array('menu_order' => $index), array('ID' => $post_id));
}
wp_die();
}
// Modify the query on the admin page for dynamically registered CPTs
add_action('pre_get_posts', 'cpt_custom_order');
function cpt_custom_order($query) {
if (!is_admin() || !$query->is_main_query()) {
return;
}
$cpt_data = get_option('custom_cpt_data', []);
$cpt_names = array_column($cpt_data, 'name');
if (in_array($query->get('post_type'), $cpt_names)) {
$query->set('orderby', 'menu_order');
$query->set('order', 'ASC');
}
}
// Modify the front-end query for dynamically registered CPTs
add_action('pre_get_posts', 'modify_cpt_query_on_front_end');
function modify_cpt_query_on_front_end($query) {
if (!is_admin() && $query->is_main_query()) {
$cpt_data = get_option('custom_cpt_data', []);
$cpt_names = array_column($cpt_data, 'name');
if (is_post_type_archive($cpt_names)) {
$query->set('orderby', 'menu_order');
$query->set('order', 'ASC');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment