Last active
December 23, 2023 06:05
-
-
Save jayeshhpatel/d33f35e7ae958ffdaae7c83823d92723 to your computer and use it in GitHub Desktop.
DataTables Server Side Processing in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add the following piece of code to your functions.php file. | |
add_shortcode('getMembership-lists', 'ajax_membership_table_shortcode'); | |
// WITH AJAX LOAD TABLE | |
function membership_datatables_scripts() { | |
wp_register_style('datatables_style', '//cdn.datatables.net/1.11.1/css/jquery.dataTables.min.css'); | |
wp_enqueue_style('datatables_style'); | |
wp_register_script('datatables', '//cdn.datatables.net/1.11.1/js/jquery.dataTables.min.js', array('jquery'), true); | |
wp_enqueue_script('datatables'); | |
wp_enqueue_script( 'membership_datatables', get_stylesheet_directory_uri(). '/js/membershiptable.js', array(), '1.0', true ); | |
wp_localize_script( 'membership_datatables', 'ajax_url_membership', admin_url('admin-ajax.php?action=membership_datatables') ); | |
} | |
function ajax_membership_table_shortcode() { | |
membership_datatables_scripts(); | |
ob_start(); ?> | |
<div class="container-membership"> | |
<table id="membership" class="display compact table-membership" style="width:100%"> | |
<thead> | |
<tr> | |
<th>Membership Number</th> | |
<th>Institute Name</th> | |
<th>Email Address</th> | |
<th>Contact Status Code</th> | |
</tr> | |
</thead> | |
</table> | |
</div> | |
<?php return ob_get_clean(); | |
} | |
add_action('wp_ajax_membership_datatables', 'datatables_server_side_callback'); | |
add_action('wp_ajax_nopriv_membership_datatables', 'datatables_server_side_callback'); | |
function datatables_server_side_callback() { | |
header("Content-Type: application/json"); | |
$request= $_GET; | |
$columns = array( | |
0 => 'member_id', | |
1 => 'post_title', | |
2 => 'member_email', | |
3 => 'member_code' | |
); | |
$args = array( | |
'post_type' => 'member', | |
'post_status' => 'publish', | |
'posts_per_page' => $request['length'], | |
'offset' => $request['start'], | |
'order' => $request['order'][0]['dir'], | |
); | |
if ($request['order'][0]['column'] == 1) { | |
$args['orderby'] = $columns[$request['order'][0]['column']]; | |
} else { | |
$args['orderby'] = 'meta_value_num'; | |
$args['meta_key'] = $columns[$request['order'][0]['column']]; | |
} | |
//$request['search']['value'] <= Value from search | |
$search_val = $request['search']['value']; | |
if( !empty($search_val) ) { // When datatables search is used | |
$args['meta_query'] = array( | |
'relation' => 'OR', | |
array( | |
'key' => 'd_title', | |
'value' => sanitize_text_field($search_val), | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'member_id', | |
'value' => sanitize_text_field($search_val), | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'member_email', | |
'value' => sanitize_text_field($search_val), | |
'compare' => 'LIKE' | |
), | |
array( | |
'key' => 'member_code', | |
'value' => sanitize_text_field($search_val), | |
'compare' => 'LIKE' | |
) | |
); | |
} | |
$membership_query = new WP_Query($args); | |
$totalData = $membership_query->found_posts; | |
if ( $membership_query->have_posts() ) { | |
while ( $membership_query->have_posts() ) { | |
$membership_query->the_post(); | |
$nestedData = array(); | |
$nestedData[] = get_field('member_id'); | |
$nestedData[] = get_the_title(); | |
$nestedData[] = get_field('member_email'); | |
$nestedData[] = '<span class="status">'.get_field('member_code').'</span>'; | |
$data[] = $nestedData; | |
} | |
wp_reset_query(); | |
$json_data = array( | |
"draw" => intval($request['draw']), | |
"recordsTotal" => intval($totalData), | |
"recordsFiltered" => intval($totalData), | |
"data" => $data | |
); | |
echo json_encode($json_data); | |
} else { | |
$json_data = array( | |
"data" => array() | |
); | |
echo json_encode($json_data); | |
} | |
wp_die(); | |
} | |
Source: | |
- https://itsmereal.com/datatables-server-side-processing-in-wordpress/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment