Ninja Tables Shortcodes

Below, you’ll find a detailed guide on how to add the Ninja Tables Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Ninja Tables Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Ninja Tables Plugin and the shortcodes it provides:

Plugin Icon
Ninja Tables – Best Data Table Plugin for WordPress

"Ninja Tables is the premier data table plugin for WordPress, designed to provide seamless data management. Its functionality enhances site performance with efficient, user-friendly features."

★★★★☆ (400) Active Installs: 80000+ Tested with: 6.3.2 PHP Version: 7.4
Included Shortcodes:
  • [ninja_table]
  • [ninja_table_info]
  • [ninja_table_cell]
  • [ninja_table_builder]

Ninja Tables [ninja_table] Shortcode

The Ninja Tables plugin shortcode is a powerful tool that renders a table. It takes attributes and content to generate a table with specific settings. It allows customization of table ID, filter, width, and additional info. It supports formulas and can be integrated with LiteSpeed cache for faster loading. It initiates the table rendering process and returns the final output.

Shortcode: [ninja_table]

Parameters

Here is a list of all possible renderTableShortcode shortcode parameters and attributes:

  • id – Unique identifier of the table to be rendered
  • filter – Allows for filtering the data within the table
  • use_parent_width – If set, the table width matches its parent container
  • info – Additional information related to the table

Examples and Usage

Basic example – A shortcode that displays the table with the specified ID.

[ninja_table id=1 /]

Advanced examples

Displaying a table with a specified ID and using the parent’s width for the table layout.

[ninja_table id=1 use_parent_width=true /]

Displaying a table with a specified ID and applying a filter to the table data.

[ninja_table id=1 filter='column:value' /]

Displaying a table with a specified ID and providing additional information.

[ninja_table id=1 info='Extra table information' /]

Note: In the filter example, replace ‘column’ with the name of the column you wish to filter, and ‘value’ with the value you want to filter by. For instance, if you wanted to filter a table of employees to only show those in the ‘Marketing’ department, you might use filter=’department:Marketing’.

These are just a few examples of how you can use the ninja_table shortcode to display tables on your WordPress site. The shortcode is highly flexible and can be customized to suit your specific needs.

PHP Function Code

In case you have difficulties debugging what causing issues with [renderTableShortcode] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode($shortCodeBase, [$this, 'renderTableShortcode']);

Shortcode PHP function:

function renderTableShortcode($atts, $content = '')
    {
        $shortCodeDefaults = [
            'id' => false,
            'filter' => false,
            'use_parent_width' => false,
            'info' => ''
        ];

        $shortCodeDefaults = apply_filters('ninja_tables_shortcode_defaults', $shortCodeDefaults);
        $shortCodeData = shortcode_atts($shortCodeDefaults, $atts);
        $shortCodeData = apply_filters('ninja_tables_shortcode_data', $shortCodeData);

        $tableArray = $this->getTableArray($shortCodeData, $content);

        if (Arr::get($tableArray, 'settings.formula_support') == 'yes') {
            do_action('ninja_tables_require_formulajs', $tableArray);
        }

        $tableArray = apply_filters('ninja_table_js_config', $tableArray, $shortCodeData['filter']);

        if (defined('LSCWP_V')) {
            do_action('litespeed_tag_add', 'ninja_tables_light_speed_clear_cache');
        }

        ob_start();
        do_action('ninja_tables-render-table-' . Arr::get($tableArray, 'settings.library'), $tableArray);

        return ob_get_clean();
    }

Code file location:

ninja-tables/ninja-tables/app/Hooks/Handlers/PublicDataHandler.php

Ninja Tables [ninja_table_info] Shortcode

The Ninja Tables shortcode is a powerful tool that retrieves and displays specific information about a table. It accepts two parameters: ‘id’ and ‘field’. The ‘id’ represents the table’s ID, while the ‘field’ can be ‘title’, ‘description’, ‘total_rows’, ‘last_modified’, or ‘last_editor’. If these parameters are valid, the shortcode will return the requested data.

Shortcode: [ninja_table_info]

Parameters

Here is a list of all possible ninja_table_info shortcode parameters and attributes:

  • id – The unique identifier of the table.
  • field – The specific detail of the table you want to display. It can be:
    • title – The title of the table.
    • description – The description of the table.
    • total_rows – The total number of rows in the table.
    • last_modified – The date and time when the table was last edited.
    • last_editor – The name of the user who last edited the table.

Examples and Usage

Basic example – The shortcode displays the title of the table with the specified ID.

[ninja_table_info id="123" field="title"]

Advanced examples

In this example, the shortcode is used to display the description of a specific table. The table is referenced by its ID, and the ‘field’ attribute is set to ‘description’.

[ninja_table_info id="456" field="description"]

Here, the shortcode is displaying the last modification time of the table. This is achieved by setting the ‘field’ attribute to ‘last_modified’.

[ninja_table_info id="789" field="last_modified"]

This example uses the shortcode to display the total number of rows in a specific table. The ‘field’ attribute is set to ‘total_rows’ to achieve this.

[ninja_table_info id="321" field="total_rows"]

Finally, this example shows how to use the shortcode to display the name of the last editor of a table. The ‘field’ attribute is set to ‘last_editor’ to achieve this.

[ninja_table_info id="654" field="last_editor"]

PHP Function Code

In case you have difficulties debugging what causing issues with [ninja_table_info] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('ninja_table_info', [$this, 'tableInfoShortcode']);

Shortcode PHP function:

function tableInfoShortcode($atts)
    {
        $shortCodeDefaults = [
            'id' => false,
            'field' => ''
        ];
        $shortCodeData = shortcode_atts($shortCodeDefaults, $atts);
        extract($shortCodeData);
        if (!$id || !$field) {
            return;
        }

        $id = absint($id);
        $table = get_post($id);
        if (!$table) {
            return;
        }

        $validFields = [
            'title',
            'description',
            'total_rows',
            'last_modified',
            'last_editor'
        ];
        if (!in_array($field, $validFields)) {
            return;
        }

        switch ($field) {
            case 'title':
                return $table->post_title;
                break;
            case 'description':
                return $table->post_content;
                break;
            case 'last_modified':
                $getLastModifiedTime = get_post_meta($table->ID, '_last_edited_time', true);
                $lastEditedTime = apply_filters('ninja_tables_last_edited_time', $getLastModifiedTime, $table->ID);
                if (!$lastEditedTime) {
                    $lastEditedTime = $table->post_modified;
                }

                return $lastEditedTime;
                break;
            case 'last_editor':
                $lastEditorId = get_post_meta($table->ID, '_last_edited_by', true);
                if (!$lastEditorId) {
                    $lastEditorId = $table->post_author;
                }
                if ($lastEditorId) {
                    $user = get_userdata($lastEditorId);
                    if ($user) {
                        return $user->display_name;
                    }
                }
                break;
            case 'total_rows':
                $total = NinjaTableItem::where('table_id', $table->ID)
                    ->count();
                if ($total) {
                    return $total;
                }

                break;
            default:
                return '';
                break;
        }

        return '';
    }

Code file location:

ninja-tables/ninja-tables/app/Hooks/Handlers/PublicDataHandler.php

Ninja Tables [ninja_table_cell] Shortcode

The Ninja Tables plugin shortcode, ‘ninja_table_cell’, allows you to retrieve and display specific cell data from a table. This shortcode uses the table ID, row, and column as parameters, returning the corresponding cell’s value. If a row ID is provided, it fetches data based on it. It also processes arrays and executes nested shortcodes.

Shortcode: [ninja_table_cell]

Parameters

Here is a list of all possible ninja_table_cell shortcode parameters and attributes:

  • id – The unique identifier for the table
  • row – Specifies the row number in the table
  • column – Specifies the column name in the table
  • row_id – Specifies the unique identifier for a row in the table

Examples and Usage

Basic example – The shortcode below displays a single cell from a table by specifying the table id, row, and column name.

[ninja_table_cell id="1" row="2" column="name"]

Advanced examples

The shortcode below retrieves a single cell from a table by specifying the table id and row id. This is useful when the row id is known, but the row number is not.

[ninja_table_cell id="1" row_id="5" column="name"]

Here, the shortcode retrieves a single cell from a table by specifying the table id, row, and column index. This is useful when the column name is not known, but the column index is known.

[ninja_table_cell id="1" row="2" column="0"]

In this example, the shortcode retrieves a single cell from a table by specifying the table id, row id, and column index. This is useful when both the row id and column index are known, but the row number and column name are not.

[ninja_table_cell id="1" row_id="5" column="0"]

PHP Function Code

In case you have difficulties debugging what causing issues with [ninja_table_cell] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('ninja_table_cell', [$this, 'tableCellShortcode']);

Shortcode PHP function:

function tableCellShortcode($atts)
    {
        $shortCodeDefaults = [
            'id' => 0,
            'row' => 0,
            'column' => '',
            'row_id' => false
        ];
        $shortCodeData = shortcode_atts($shortCodeDefaults, $atts);

        extract($shortCodeData);

        if (!$id || (!$row && !$row_id) || !$column) {
            return '';
        }

        $id = absint($id);
        $tableSettings = ninja_table_get_table_settings($id, 'public');

        if ($row_id) {
            $rowData = NinjaTableItem::where('table_id', $id)
                                ->where('id', $row_id)
                                ->first();
            if (!$rowData) {
                return '';
            }
            $data = json_decode($rowData->value, true);
            if (isset($data[$column])) {
                $value = $data[$column];
                if (is_array($value)) {
                    $value = $this->processCellInfoArray($value, $column, $id);
                }

                return do_shortcode($value);
            } else {
                $dataArray = array_values($data);

                if (isset($dataArray[$column])) {
                    $value = $data[$column];
                    if (is_array($value)) {
                        $value = $this->processCellInfoArray($value, $column, $id);
                    }

                    return do_shortcode($value);
                }
            }

            return '';
        }

        $tableColumns = ninja_table_get_table_columns($id, 'public');
        $data = ninjaTablesGetTablesDataByID($id, $tableColumns, $tableSettings['default_sorting'], false, 1, $row - 1);

        if ($data) {
            $data = $data[0];
        } else {
            return '';
        }

        $content = '';
        if (isset($data[$column])) {
            $content = $data[$column];
        } else {
            $arrayValues = array_values($data);
            if (isset($arrayValues[$column])) {
                $content = $arrayValues[$column];
            }
        }
        if (is_array($content)) {
            $content = $this->processCellInfoArray($content, $column, $id);
        }

        return do_shortcode($content);
    }

Code file location:

ninja-tables/ninja-tables/app/Hooks/Handlers/PublicDataHandler.php

Ninja Tables [ninja_table_builder] Shortcode

The Ninja Tables plugin shortcode is a powerful tool that generates dynamic tables. This shortcode retrieves a specific table by its ID, checks if it exists and is of the correct type. It then fetches the associated data, settings, and responsive design parameters from the post meta. The shortcode also enqueues necessary scripts, triggers an action before rendering, and finally returns a view with the table data, settings, and responsive parameters.

Shortcode: [ninja_table_builder]

Parameters

Here is a list of all possible ninja_table_builder shortcode parameters and attributes:

  • id – The unique identifier of the Ninja table

Examples and Usage

Basic example – A Ninja Table can be inserted into a post or page using the shortcode and specifying the table’s ID.

[ninja_table_builder id=1 /]

Advanced examples

Displaying a Ninja Table with custom settings. This example assumes that you have already created a table with ID 2 and defined its settings in the ‘_ninja_table_builder_table_settings’ meta key. The table will be displayed according to these settings.

[ninja_table_builder id=2 /]

Displaying a Ninja Table with responsive design. This example assumes that you have already created a table with ID 3 and defined its responsive design in the ‘_ninja_table_builder_table_responsive’ meta key. The table will be displayed responsively according to these settings.

[ninja_table_builder id=3 /]

PHP Function Code

In case you have difficulties debugging what causing issues with [ninja_table_builder] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('ninja_table_builder', [$this, 'ninjaTableBuilderShortCode']);

Shortcode PHP function:

function ninjaTableBuilderShortCode($atts = [], $content = null, $tag = '')
    {
        // normalize attribute keys, lowercase
        $atts = array_change_key_case((array)$atts, CASE_LOWER);
        // override default attributes with user attributes
        $short_code_atts = shortcode_atts([
            'id' => null,
        ], $atts, $tag);
        $id = $short_code_atts['id'];
        $table_id = absint($id);
        if (!$table_id) {
            return;
        }
        $table = get_post($table_id);

        if (!$table || $table->post_type != 'ninja-table') {
            return;
        }
        $ninja_table_builder_html = get_post_meta($table->ID, '_ninja_table_builder_table_html', true);
        $ninja_table_builder_table_data = get_post_meta($table->ID, '_ninja_table_builder_table_data', true);
        $ninja_table_builder_setting = get_post_meta($table->ID, '_ninja_table_builder_table_settings', true);
        $ninja_table_builder_responsive = get_post_meta($table->ID, '_ninja_table_builder_table_responsive', true);
        $html = do_shortcode($ninja_table_builder_html);
        $this->enqueueNinjaTableBuilderScript();

        do_action('ninja_table_builder_before_render', $table_id);

        $app = App::getInstance();

        return $app->view->make('public/drag-and-drop-html', [
            'ninja_table_builder_html' => $html,
            'table_data'               => $ninja_table_builder_table_data,
            'setting'                  => $ninja_table_builder_setting,
            'responsive'               => $ninja_table_builder_responsive,
            'table_id'                 => $table_id
        ]);
    }

Code file location:

ninja-tables/ninja-tables/app/Hooks/Handlers/PublicDataHandler.php

Conclusion

Now that you’ve learned how to embed the Ninja Tables Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *