Skip to content

Instantly share code, notes, and snippets.

@techjewel
Created September 3, 2018 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save techjewel/5e2586b3846631dcfab2ea1f73ddc36b to your computer and use it in GitHub Desktop.
Save techjewel/5e2586b3846631dcfab2ea1f73ddc36b 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
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();
});
@arubelbd
Copy link

activate but not show dashboard why ?

@aladin2016
Copy link

Hello ! i use this plugin but i have 1 problem ... how can I make it appear to me only that product with that number? for example I want to look for number 2 but I show all the products that contain the number 2?... and I just want to show me only one product with that code not all that contain 2
for example when I'm looking for 2 I see the code 00002 + 00023+00032 but I want to appear only 00002 not all that contain the number 2
sorry for my english ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment