Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hemusyl/5109868342da5bdc7527c9005205cd19 to your computer and use it in GitHub Desktop.
Save hemusyl/5109868342da5bdc7527c9005205cd19 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Ninja Table Dynamic Data
Description: Dynamic Data Example For Ninja Tables
Version: 1.0.0
Author: WPManageNinja Shahjahan Jewel
Author URI: https://wpmanageninja.com/
Plugin URI: https://wpmanageninja.com/downloads/ninja-tables-pro-add-on/
License: GPLv2 or later
Text Domain: ninja_table_dynamic_data
Domain Path: /resources/languages
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
die;
}
class NinjaTableDynamicPostData
{
/**
* Hook For Ninja Table Data Fetching Function
*/
public function boot() {
add_filter('ninja_tables_get_raw_table_data', array($this, 'filterTableData'), 10, 2);
}
/**
* @param $originalData array
* @param $tableId int
*
* In this function we are checking if the table_id is 181 and if yes then we are altering the data and
* return our modified data.
*
* @return array
*/
public function filterTableData($originalData, $tableId) {
if($tableId == 181) {
$columnsKeys = $this->getColumnKeys($tableId);
// Now Fetch Your Dynamic Data from database
// We are fetching first 10 records from wp_posts table and returning the data
global $wpdb;
$posts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts LIMIT 50");
$formattedData = array();
foreach ($posts as $post) {
$formattedData[] = array_merge($columnsKeys, array(
'title' => ucwords($post->post_title),
'status' => ucfirst($post->post_status),
'url' => "<a href='".get_permalink($post)."' target='_blank'>View</a>",
'post_type' => ucfirst($post->post_type)
));
}
return $formattedData;
}
return $originalData; // Don't forget to return data for other tables. It's Important
}
/**
* @param $tableId int
*
* Get Table Column Keys as array keys.
*
* @return array
*/
private function getColumnKeys($tableId) {
$columns = ninja_table_get_table_columns($tableId);
$columnsKeys = array();
foreach ($columns as $column) {
$columnsKeys[$column['key']] = '';
}
return $columnsKeys;
}
}
add_action('plugins_loaded', function () {
$NinjaTableDynamicData = new NinjaTableDynamicPostData();
$NinjaTableDynamicData->boot();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment