WPdatatables Shortcodes

Below, you’ll find a detailed guide on how to add the WPdatatables 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 Wpdatatables Plugin shortcodes not to show or not to work correctly.

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

Plugin Icon
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin

"wpDataTables is a WordPress plugin designed to create dynamic tables and table charts. It simplifies data management, making your website more interactive and user-friendly."

★★★★✩ (353) Active Installs: 70000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [wpdatatable]
  • [wpdatachart]
  • [wpdatatable_cell]

Wpdatatables [wpdatatable] Shortcode

The wpdatatable shortcode is utilized to render a table from the WPDataTables plugin on a WordPress page. The shortcode: [wpdatatable] fetches a table’s data using its ID. The function ‘wdtWpDataTableShortcodeHandler’ handles the shortcode, extracts attributes, checks if the table ID exists, and then fetches the table data. If the table is of ‘simple’ type, it generates the table directly. Otherwise, it prepares the column data, fills the table, and generates it. The function also handles exceptions and displays errors if any.

Shortcode: [wpdatatable]

Parameters

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

  • id – The unique identifier of the wpDataTable
  • var1 – Optional variable to add additional data in the wpDataTable
  • var2 – Another optional variable to input more data in the wpDataTable
  • var3 – A third optional variable to insert extra information in the wpDataTable
  • export_file_name – Name of the file for exporting the wpDataTable
  • table_view – Defines the view mode of the wpDataTable

Examples and Usage

Basic example – A straightforward way to use the wpdatatable shortcode is by simply specifying the table id. This will display the table with the specified id on your page.

[wpdatatable id=1 /]

Advanced examples

Let’s say you want to display a table and also pass some variables to it. You can do this by using the var1, var2, and var3 attributes. Here’s an example:

[wpdatatable id=1 var1="value1" var2="value2" var3="value3" /]

Another advanced usage of the shortcode involves specifying the export file name. This can be useful if you want to allow users to download the table data. Here’s how you can do this:

[wpdatatable id=1 export_file_name="my_table_data" /]

Finally, you can also specify the table view. This can be either ‘regular’ or ‘simple’. Here’s an example:

[wpdatatable id=1 table_view="simple" /]

PHP Function Code

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

Shortcode line:

add_shortcode('wpdatatable', 'wdtWpDataTableShortcodeHandler');

Shortcode PHP function:

function wdtWpDataTableShortcodeHandler($atts, $content = null) {
    global $wdtVar1, $wdtVar2, $wdtVar3, $wdtExportFileName;

    extract(shortcode_atts(array(
        'id' => '0',
        'var1' => '%%no_val%%',
        'var2' => '%%no_val%%',
        'var3' => '%%no_val%%',
        'export_file_name' => '%%no_val%%',
        'table_view' => 'regular'
    ), $atts));

	$id = absint($id);

    if (is_admin() && defined( 'AVADA_VERSION' ) && is_plugin_active('fusion-builder/fusion-builder.php') &&
        class_exists('Fusion_Element') && class_exists('WPDataTables_Fusion_Elements') &&
        isset($_POST['action']) && $_POST['action'] === 'get_shortcode_render')
    {
        return WPDataTables_Fusion_Elements::get_content_for_avada_live_builder($atts, 'table');
    }

    if (!$id) {
        return false;
    }

    do_action('wpdatatables_before_render_table_config_data', $id);

    $tableData = WDTConfigController::loadTableFromDB($id);
    if (empty($tableData->content)) {
        return esc_html__('wpDataTable with provided ID not found!', 'wpdatatables');
    }

    do_action('wpdatatables_before_render_table', $id);

    /** @var mixed $var1 */
    $wdtVar1 = $var1 !== '%%no_val%%' ? $var1 : $tableData->var1;
    /** @var mixed $var2 */
    $wdtVar2 = $var2 !== '%%no_val%%' ? $var2 : $tableData->var2;
    /** @var mixed $var3 */
    $wdtVar3 = $var3 !== '%%no_val%%' ? $var3 : $tableData->var3;

    /** @var mixed $export_file_name */
    $wdtExportFileName = $export_file_name !== '%%no_val%%' ? $export_file_name : '';

    if ($tableData->table_type === 'simple'){
        try {
            $wpDataTableRows = WPDataTableRows::loadWpDataTableRows($id);
            $output = $wpDataTableRows->generateTable($id);
        } catch (Exception $e) {
            $output = ltrim($e->getMessage(), '<br/><br/>');
        }
    } else {

	    $wpDataTable = new WPDataTable();

        $wpDataTable->setWpId($id);

        $columnDataPrepared = $wpDataTable->prepareColumnData($tableData);

        try {
            $wpDataTable->fillFromData($tableData, $columnDataPrepared);
            $wpDataTable = apply_filters('wpdatatables_filter_initial_table_construct', $wpDataTable);

            $output = '';
            if ($tableData->show_title && $tableData->title) {
                $output .= apply_filters('wpdatatables_filter_table_title', (empty($tableData->title) ? '' : '<h3 class="wpdt-c" id="wdt-table-title-'. $id .'">' . $tableData->title . '</h3>'), $id);
            }
            if ($tableData->show_table_description && $tableData->table_description) {
                $output .= apply_filters('wpdatatables_filter_table_description_text', (empty($tableData->table_description) ? '' : '<p class="wpdt-c" id="wdt-table-description-'. $id .'">' . $tableData->table_description . '</p>'), $id);
            }
            $output .= $wpDataTable->generateTable();
        } catch (Exception $e) {
            $output = WDTTools::wdtShowError($e->getMessage());
        }
    }
    $output = apply_filters('wpdatatables_filter_rendered_table', $output, $id);

    return $output;
}

Code file location:

wpdatatables/wpdatatables/wpdatatables.php

Wpdatatables [wpdatachart] Shortcode

The wpDataChart shortcode is a powerful tool within the wpDataTables plugin. It fetches and displays a specific chart based on the ID provided. The shortcode checks if the chart exists in the database, and if so, it renders the chart. If the chart doesn’t exist or an error occurs, it returns an informative message.

Shortcode: [wpdatachart]

Parameters

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

  • id – The unique identifier of the wpDataChart to be displayed.

Examples and Usage

Basic example – A straightforward usage of the shortcode to display a wpDataChart by its ID.

[wpdatachart id=1 /]

Advanced examples

Displaying a wpDataChart by its ID, but with error handling. If the chart with the provided ID is not found, it will display an error message.


function display_wpdatachart($id) {
    $shortcode = '[wpdatachart id=' . $id . ' /]';
    $output = do_shortcode($shortcode);
    if ($output === false) {
        return 'wpDataChart with provided ID not found!';
    }
    return $output;
}
echo display_wpdatachart(1);

In the above example, the function display_wpdatachart is created to handle the shortcode output. If the shortcode returns false, indicating that the chart with the provided ID is not found, it will return an error message. Otherwise, it will return the output of the shortcode, which is the rendered chart.

Another advanced usage is to use the shortcode within a post or page content. This can be achieved by adding the shortcode in the content editor of the post or page.


add_filter('the_content', 'add_wpdatachart_to_content');
function add_wpdatachart_to_content($content) {
    if (is_single()) {
        $content .= '[wpdatachart id=1 /]';
    }
    return $content;
}

In this example, the ‘the_content’ filter is used to add the shortcode at the end of the content of a single post or page. When the post or page is viewed, the chart with the ID 1 will be displayed at the end of the content.

PHP Function Code

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

Shortcode line:

add_shortcode('wpdatachart', 'wdtWpDataChartShortcodeHandler');

Shortcode PHP function:

function wdtWpDataChartShortcodeHandler($atts, $content = null) {
    extract(shortcode_atts(array(
        'id' => '0'
    ), $atts));

	$id = absint($id);

    if (is_admin() && defined( 'AVADA_VERSION' ) && is_plugin_active('fusion-builder/fusion-builder.php') &&
        class_exists('Fusion_Element') && class_exists('WPDataTables_Fusion_Elements') &&
        isset($_POST['action']) && $_POST['action'] === 'get_shortcode_render')
    {
        return WPDataTables_Fusion_Elements::get_content_for_avada_live_builder($atts, 'chart');
    }

    /** @var mixed $id */
    if (!$id) {
        return false;
    }

    try {
        $wpDataChart = new WPDataChart();
        $wpDataChart->setId($id);
        $wpDataChart->loadFromDB();

        $chartExists = $wpDataChart->getwpDataTableId();
        if (empty($chartExists)) {
            return esc_html__('wpDataChart with provided ID not found!', 'wpdatatables');
        }

        do_action('wpdatatables_before_render_chart', $wpDataChart->getId());

        return $wpDataChart->renderChart();
    } catch (Exception $e) {
        return esc_html__('wpDataTables encountered an issue trying to display chart. Please edit the chart in the admin area for more details.');
    }
}

Code file location:

wpdatatables/wpdatatables/wpdatatables.php

Wpdatatables [wpdatatable_cell] Shortcode

The wpdatatable_cell shortcode is a powerful tool that allows you to retrieve and display specific cell data from a wpDataTable. It uses parameters like table_id, row_id, and column_key to pinpoint the exact cell.

Shortcode: [wpdatatable_cell]

Parameters

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

  • table_id – The unique identifier of the table
  • row_id – The unique identifier of the row
  • column_key – The unique identifier of the column
  • column_id – The unique identifier of the column for value retrieval
  • column_id_value – The value that corresponds to the column_id
  • sort – This parameter is used to sort the table data; 1 for sort and 0 for no sort

Examples and Usage

Basic example – Displaying a specific cell from a wpDataTable using the table ID, row ID, and column key.

[wpdatatable_cell table_id="1" row_id="2" column_key="name"]

Advanced examples

Displaying a specific cell from a wpDataTable using the table ID, row ID, column key, and sorting. The sort attribute is set to ‘1’, which means the table will be sorted.

[wpdatatable_cell table_id="1" row_id="2" column_key="name" sort="1"]

Displaying a specific cell from a wpDataTable using the table ID, column ID, column ID value, and column key. This example is useful when you want to display a specific cell based on a unique identifier in another column.

[wpdatatable_cell table_id="1" column_id="user_id" column_id_value="1001" column_key="name"]

Using the shortcode to display a specific cell value from a wpDataTable by referencing the table ID, row ID, and column key. If the cell value is not found, it will return a message ‘wpDataTable with provided ID not found!’

[wpdatatable_cell table_id="1" row_id="2" column_key="name"]

PHP Function Code

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

Shortcode line:

add_shortcode('wpdatatable_cell', 'wdtWpDataTableCellShortcodeHandler');

Shortcode PHP function:

function wdtWpDataTableCellShortcodeHandler($atts, $content = null) {
    global $wpdb;
    extract(shortcode_atts(array(
        'table_id' => '0',
        'row_id' => '0',
        'column_key' => '%%no_val%%',
        'column_id' => '%%no_val%%',
        'column_id_value' => '%%no_val%%',
        'sort' => '1'
    ), $atts));

	$table_id = absint($table_id);
	$row_id = absint($row_id);
	$sort = absint($sort);

    if (!$table_id)
        return esc_html__('wpDataTable with provided ID not found!', 'wpdatatables');

    /** @var int $row_id */
    $rowID = !$row_id ? 0 : $row_id;

    /** @var int $sort */
    $includeSort = $sort == 1;

    /** @var mixed $column_key */
    $columnKey = $column_key !== '%%no_val%%' ? $column_key : '';

    /** @var mixed $column_id */
    $columnID = $column_id !== '%%no_val%%' ? $column_id : '';

    /** @var mixed $column_id_value */
    $columnIDValue = $column_id_value !== '%%no_val%%' ? $column_id_value : '';

    $rowID         = apply_filters('wpdatatables_cell_filter_row_id', $rowID, $columnKey, $columnID, $columnIDValue, $table_id);
    $columnKey     = apply_filters('wpdatatables_cell_filter_column_key', $columnKey, $rowID, $columnID, $columnIDValue, $table_id);
    $columnID      = apply_filters('wpdatatables_cell_filter_column_id', $columnID, $columnKey, $rowID, $columnIDValue, $table_id);
    $columnIDValue = apply_filters('wpdatatables_cell_filter_column_id_value', $columnIDValue, $columnKey, $rowID, $columnID, $table_id);

    if ($columnKey == '')
        return esc_html__('Column key for provided table ID not found!', 'wpdatatables');

    $tableData = WDTConfigController::loadTableFromDB($table_id, false);

    if (empty($tableData->content))
        return esc_html__('wpDataTable with provided ID not found!', 'wpdatatables');

    if ($tableData->table_type === 'simple') {

        if ($columnIDValue != '' || $columnID != '')
            return esc_html__('For getting cell value from simple table, column_id and column_id_value are not supported. Please use row_id.', 'wpdatatables');

        if ($rowID == 0)
            return esc_html__('Row ID for provided table ID not found!', 'wpdatatables');

        try {
            $wpDataTableRows = WPDataTableRows::loadWpDataTableRows($table_id);
            $rowsData = $wpDataTableRows->getRowsData();
            $columnHeaders = array_flip($wpDataTableRows->getColHeaders());
            $columnKey = strtoupper($columnKey);
            if (isset($columnHeaders[$columnKey])) {
                $rowID = $rowID - 1;
                if(isset($rowsData[$rowID])){
                    $columnKey = $columnHeaders[$columnKey];
                    $cellMetaClasses = array_unique($wpDataTableRows->getCellClassesByIndexes($rowsData, $rowID, $columnKey));
                    $cellValue = $wpDataTableRows->getCellDataByIndexes($rowsData, $rowID, $columnKey);
                    $cellValue = apply_filters('wpdatatables_cell_value_filter', $cellValue, $columnKey, $rowID, $columnID, $columnIDValue, $table_id);
                    $cellValueOutput = WPDataTableRows::prepareCellDataOutput($cellValue, $cellMetaClasses, $rowID, $columnKey, $table_id);
                } else {
                    return esc_html__('Row ID for provided table ID not found!', 'wpdatatables');
                }
            } else {
                return esc_html__('Column key for provided table ID not found!', 'wpdatatables');
            }
        } catch (Exception $e) {
            return ltrim($e->getMessage(), '<br/><br/>');
        }
    } else {
        try {
            $wpDataTable = WPDataTable::loadWpDataTable($table_id);

            if (!isset($wpDataTable->getWdtColumnTypes()[$columnKey]))
                return esc_html__('Column key for provided table ID not found!', 'wpdatatables');

            if ($columnIDValue != '' || $columnID != ''){
                if ($columnID == '')
                    return esc_html__('Column ID for provided table ID not found!', 'wpdatatables');

                if ($columnIDValue == '')
                    return esc_html__('Column ID value for provided table ID not found!', 'wpdatatables');

                if (!isset($wpDataTable->getWdtColumnTypes()[$columnID]))
                    return esc_html__('Column ID for provided table ID not found!', 'wpdatatables');

                if (in_array($wpDataTable->getWdtColumnTypes()[$columnID],['date','datetime','time','float']))
                    return esc_html__('At the moment float, date, datetime and time columns can not be used as column_id. Please use other column that contains unique identifiers.', 'wpdatatables');

                if ($columnKey == $columnID)
                    return esc_html__('Column Key an Column ID can not be the same!', 'wpdatatables');
            }

            $isTableSortable = $wpDataTable->sortEnabled();
            if ($includeSort && $isTableSortable){
                $sortDirection = $wpDataTable->getDefaultSortDirection();
                if ( $wpDataTable->getDefaultSortColumn()){
                    $sortColumn = $wpDataTable->getColumns()[$wpDataTable->getDefaultSortColumn()]->getOriginalHeader();
                    $columnType = $wpDataTable->getColumns()[$wpDataTable->getDefaultSortColumn()]->getDataType();
                } else {
                    $sortColumn = $wpDataTable->getColumns()[0]->getOriginalheader();
                    $columnType = $wpDataTable->getColumns()[0]->getDataType();
                }
            }

            $dataRows = $wpDataTable->getDataRows();
            if ($dataRows == [])
                return esc_html__('Table do not have data for provided table ID!', 'wpdatatables');
            if ($includeSort && $isTableSortable){
                $sortDirection = $sortDirection == 'ASC' ? SORT_ASC : SORT_DESC;
                $sortingType = in_array($columnType, array('float', 'int')) ? SORT_NUMERIC : SORT_REGULAR;
                array_multisort(
                    array_column($dataRows, $sortColumn),
                    $sortDirection,
                    $sortingType,
                    $dataRows
                );
            }

            $dataRows = apply_filters('wpdatatables_cell_data_rows_filter', $dataRows, $columnKey, $rowID, $columnID, $columnIDValue, $table_id);

            if ($columnIDValue != '' || $columnID != '') {
                $filteredData = array_filter($dataRows, function ($item) use ($columnIDValue, $columnID) {
                    if ($item[$columnID] == $columnIDValue) {
                        return true;
                    }
                    return false;
                });
                if ($filteredData == [])
                    return esc_html__('Column ID value for provided table ID not found!', 'wpdatatables');
                $dataRows = array_values($filteredData);
                $dataRows = apply_filters('wpdatatables_cell_filtered_data_rows_filter', $dataRows, $columnKey, $rowID, $columnID, $columnIDValue, $table_id);
                $cellValue = $dataRows[0][$columnKey];
            } else {
                if ($tableData->table_type == 'forminator') {
                    $entryIdName = 'entryid';
                    if (!isset($dataRows[0][$entryIdName]))
                        return esc_html__('Entry ID not found! Please provide existing entry id from form.', 'wpdatatables');
                    $rowID = str_replace(array('.', ','), '' , $rowID);
                    $filteredData = array_filter($dataRows, function ($item) use ($rowID, $entryIdName) {
                        if ($item[$entryIdName] == $rowID) {
                            return true;
                        }
                        return false;
                    });
                    if ($filteredData == [])
                        return esc_html__('Entry ID value for provided table ID not found!', 'wpdatatables');
                    $dataRows = array_values($filteredData);
                    $dataRows = apply_filters('wpdatatables_cell_filtered_data_rows_filter', $dataRows, $columnKey, $rowID, $columnID, $columnIDValue, $table_id);
                    $cellValue = $dataRows[0][$columnKey];
                } else {
                    $rowID = $rowID != 0 ? $rowID - 1 : 0;
                    $wpDataTable->setDataRows($dataRows);
                    $cellValue = $wpDataTable->getCell($columnKey, $rowID);
                }

            }

            $cellValue = apply_filters('wpdatatables_cell_value_filter', $cellValue, $columnKey, $rowID, $columnID, $columnIDValue, $table_id);
            $cellValueOutput = $wpDataTable->getColumn($columnKey)->prepareCellOutput($cellValue);
        } catch (Exception $e) {
            return ltrim($e->getMessage(), '<br/><br/>');
        }
    }

    return apply_filters('wpdatatables_cell_output_filter', $cellValueOutput, $cellValue, $columnKey, $rowID, $columnID, $columnIDValue, $table_id);
}

Code file location:

wpdatatables/wpdatatables/wpdatatables.php

Conclusion

Now that you’ve learned how to embed the Wpdatatables 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 *