### Example MySQL Query with Placeholders Source: https://wpdatatables.com/documentation/table-features/using-placeholders This example demonstrates how to use various placeholders within a MySQL query to filter data dynamically. ```sql SELECT * FROM my_table WHERE my_field > %VAR1% AND my_field < %VAR2% AND user_id = %CURRENT_USER_ID% ``` -------------------------------- ### Formula Syntax Example Source: https://wpdatatables.com/documentation/column-features/formula-calculated-columns This is an example of a formula that can be used in wpDataTables. It demonstrates the use of column references, arithmetic operations, and trigonometric functions. Only numeric columns can be used; non-numeric columns are treated as 0. ```plaintext col1*((col2+2)-col3*sin(col4-3)) ``` -------------------------------- ### Basic SQL Query for wpDataTable Source: https://wpdatatables.com/documentation/creating-wpdatatables/creating-mysql-based-wpdatatables-with-server-side-processing This is a fundamental SQL query used to fetch all data from a MySQL table. It's a starting point for creating a wpDataTable with server-side processing. ```sql SELECT * FROM sample_large_table ``` -------------------------------- ### Shortcode with Overridden Variable Values Source: https://wpdatatables.com/documentation/table-features/using-placeholders This shortcode example shows how to override the default values for %VAR1% and %VAR2% directly within the shortcode attributes. ```html [ wpdatatable id=12 var1=150 var2=350] ``` -------------------------------- ### wpdatatables_before_render_table Source: https://wpdatatables.com/documentation/information-for-developers/hooks This action is executed before the table starts to render, regardless of whether it's rendered via a shortcode or directly. ```APIDOC ## wpdatatables_before_render_table( $tableId ) ### Description This action is executed before the table starts to render (either with or without a shortcode). ### Parameters * **$tableId** (int) - The ID of the table. ``` -------------------------------- ### Global Page Search Shortcode with Custom Button Style Source: https://wpdatatables.com/documentation/table-features/global-page-search-shortcode This example shows how to apply a specific class, `wpdt-custom-button`, to the search button to utilize custom styling provided by the theme. This ensures the button matches the theme's design. ```shortcode [wpdatatables_page_search use_button="1" button_class="wpdt-custom-button"] ``` -------------------------------- ### wpdatatables_before_render_chart Source: https://wpdatatables.com/documentation/information-for-developers/hooks This action is executed before a chart starts to render. ```APIDOC ## wpdatatables_before_render_chart( $chartId ) ### Description This action is executed before a chart starts to render. ### Parameters * **$chartId** (int) - The table identifier from the MySQL table. ``` -------------------------------- ### Alternative Cron Job Command with wget Source: https://wpdatatables.com/documentation/table-features/caching-data-and-auto-update-cache-in-wordpress-tables An alternative cron job command using wget for triggering cache updates. Ensure wget is installed on your server. Replace placeholders. ```bash */15 * * * * wget -q -O – "your.website.url/wp-admin/admin-ajax.php?action=wdtable_update_cache&wdtable_cache_verify=your.cache.verify.string" ``` -------------------------------- ### Google Sheets Formula Example Source: https://wpdatatables.com/documentation/creating-wpdatatables/creating-wpdatatables-from-google-spreadsheets/creating-tables-wordpress-google-spreadsheets-formulas This is an example of a basic formula used in a Google Sheet. Ensure you name your sheets and ranges correctly for use with IMPORTRANGE. ```google-sheets =(H6-G6)/H6) ``` -------------------------------- ### Cron Job Command with GET Request Source: https://wpdatatables.com/documentation/table-features/caching-data-and-auto-update-cache-in-wordpress-tables A simple cron job command that uses a GET request to update the cache. Output is redirected to /dev/null to prevent unwanted logs. Replace placeholders. ```bash GET 'your.website.url/wp-admin/admin-ajax.php?action=wdtable_update_cache&wdtable_cache_verify=your.cache.verify.string' > /dev/null ``` -------------------------------- ### Global Page Search Shortcode with Custom Classes Source: https://wpdatatables.com/documentation/table-features/global-page-search-shortcode This example demonstrates how to add custom CSS classes to the search input field and button for advanced styling. The `class` attribute targets the input, and `button_class` targets the button. ```shortcode [wpdatatables_page_search placeholder="Search all tables" use_button="1" button_placeholder="Search button" use_global_search_only="1" class ="placeholder-class" button_class="button-placeholder-class"] ``` -------------------------------- ### Configure Foreign Key Source Table and Display Value Source: https://wpdatatables.com/documentation/front-end-editing/configuring-table-relations-foreign-key In the modal for foreign key configuration, choose the source wpDataTable and the column whose values should be displayed. ```javascript **Choose a source wpDataTable __** **Display value** ``` -------------------------------- ### Fetch and Serialize WordPress Data for wpDataTable Source: https://wpdatatables.com/documentation/creating-wpdatatables/creating-wpdatatables-from-serialized-php-array This PHP script initializes the WordPress environment, queries for specific post types (pages) under a parent ID, and formats the results into a serialized array. It ensures a 200 OK HTTP status and prepares data including ID, title, and a content preview with a link. Use this script as a standalone file to provide data to wpDataTable. ```php 'page', // We only want pages 'post_parent' => 244, // We only want children of a defined post ID 'post_count' => -1 // We do not want to limit the post count // We can define any additional arguments that we need - see Codex for the full list ) ); $return_array = array(); // Initializing the array that will be used for the table while( $the_query->have_posts() ){ // Fetch the post $the_query->the_post(); // Filling in the new array entry $return_array[] = array( 'Id' => get_the_id(), // Set the ID 'Title' => get_the_title(), // Set the title 'Content preview with link' => get_permalink().'||'.strip_tags( strip_shortcodes( substr( get_the_content(), 0, 200 ) ) ).'...' // Get first 200 chars of the content and replace the shortcodes and tags ); } // Now the array is prepared, we just need to serialize and output it echo serialize( $return_array ); ?> ``` -------------------------------- ### Create MySQL-based wpDataTable: Customers Source: https://wpdatatables.com/documentation/front-end-editing/configuring-table-relations-foreign-key Use this SQL query to create a wpDataTable based on the 'customers' table from a MySQL database. ```sql SELECT * FROM customers; ``` -------------------------------- ### wpdatatables_filter_server_side_data Source: https://wpdatatables.com/documentation/information-for-developers/filters This filter is applied to the JSON object with the data returned for an AJAX request for tables with server-side processing. It receives the JSON output, table ID, and GET parameters. ```APIDOC ## wpdatatables_filter_server_side_data( $json, $tableId, $_GET ) ### Description This filter is applied to the JSON object with the data returned for an AJAX request for tables with server-side processing. ### Parameters * **$json** (string) - The currently generated JSON output. * **$tableId** (int) - The table identifier from the database. * **$_GET** (array) - The contents of $_GET request (page, filtering and searching parameters, etc.). ``` -------------------------------- ### MySQL Query with Default Placeholder Values Source: https://wpdatatables.com/documentation/table-features/using-placeholders This shows how the query is parsed when default values are set for %VAR1%, %VAR2%, and %CURRENT_USER_ID%. This is useful for testing if the query returns data. ```sql SELECT * FROM my_table WHERE my_field > 0 AND my_field < 100 AND user_id = 15 ``` -------------------------------- ### Display Table with Excel-like View via Shortcode Source: https://wpdatatables.com/documentation/display-tables-excel-like-view To display a table with an Excel-like interface using a shortcode, add the 'table_view=excel' attribute to your existing shortcode. This is useful for presenting data in a familiar spreadsheet format. ```shortcode [wpdatatable id=1 table_view=excel] ``` -------------------------------- ### Column Filtering by Index via URL Source: https://wpdatatables.com/documentation/table-features/global-search-via-url Predefine filter values for a specific column by using its zero-based numerical index in the URL. For example, to filter the column with index 1 by 'Linz'. ```url ?wdt_column_filter[1]=Linz ``` -------------------------------- ### Placeholder Syntax for Master-Detail Tables Source: https://wpdatatables.com/documentation/addons/master-detail-tables Use origin headers from your table columns enclosed in percentage signs (%) as placeholders in your template page to display specific data. Ensure the column is enabled for the details section. ```html %Image% ``` -------------------------------- ### Google Charts: Customize Background and Legend Source: https://wpdatatables.com/documentation/information-for-developers/wpdatacharts-callbacks Use this jQuery snippet to set the background color and legend position for a Google Column Chart. Ensure the chart ID (38 in this example) is correct. ```javascript jQuery(window).on('load',function(){ if( typeof wpDataChartsCallbacks == 'undefined' ){ wpDataChartsCallbacks = {}; } wpDataChartsCallbacks[38] = function(obj){ obj.options.backgroundColor = '#F7F7F7'; obj.options.legend = {position:'in'}; } }); ``` -------------------------------- ### Range Filtering for Columns via URL Source: https://wpdatatables.com/documentation/table-features/global-search-via-url To apply a range filter to a column, separate the minimum and maximum values with a pipe symbol ('|') in the URL. This example filters column 3 for values between 60 and 80. ```url https://wpdatatables.com/documentation/table-features/global-search-via-url/?wdt_column_filter[3]=60|80 ``` -------------------------------- ### Passing Multiple Dynamic Placeholder Values via URL Source: https://wpdatatables.com/documentation/table-features/global-search-via-url To pass values for multiple dynamic placeholders (e.g., VAR1 and VAR2), separate them with an ampersand ('&') in the URL. ```url ?wdt_var1=your_value &wdt_var2=second_value ``` -------------------------------- ### wpdatatables_before_get_table_metadata Source: https://wpdatatables.com/documentation/information-for-developers/hooks This action is executed before the table metadata is fetched from the database. It can be used to call a function or set a global variable. ```APIDOC ## wpdatatables_before_get_table_metadata( $tableId ) ### Description This action is executed before the table metadata is fetched from the database (table settings, link to the file or the SQL query). ### Parameters * **$tableId** (int) - The ID of the table. ``` -------------------------------- ### wpdatatables_try_generate_table Source: https://wpdatatables.com/documentation/information-for-developers/hooks This action is executed when a new table is first saved and wpDataTables attempts to generate its structure and columns based on the provided data. ```APIDOC ## wpdatatables_try_generate_table( $type, $content ) ### Description This action is executed when a new table is first saved: wpDataTables tries to generate the table based on the provided data, and creates the columns and tables in the databased based on the results. ### Parameters * **$type** (string) - The table type (e.g., 'xls', 'csv', 'mysql', 'json', 'xml', 'php'). * **$content** (string) - Either a link to the data source file or a MySQL query. ``` -------------------------------- ### Create MySQL-based wpDataTable: Employees Source: https://wpdatatables.com/documentation/front-end-editing/configuring-table-relations-foreign-key Use this SQL query to create a wpDataTable based on the 'employees' table from a MySQL database. ```sql SELECT * FROM employees; ``` -------------------------------- ### wpdatatables_before_get_columns_metadata Source: https://wpdatatables.com/documentation/information-for-developers/hooks This action is executed before the columns metadata is fetched from the database. It can be used to call a function or set a global variable. ```APIDOC ## wpdatatables_before_get_columns_metadata( $tableId ) ### Description This action is executed before the columns metadata is fetched from the database (column settings). ### Parameters * **$tableId** (int) - The ID of the table. ``` -------------------------------- ### Create MySQL-based wpDataTable: Orders Source: https://wpdatatables.com/documentation/front-end-editing/configuring-table-relations-foreign-key Use this SQL query to create a wpDataTable based on the 'orders' table from a MySQL database. This table will contain foreign key IDs. ```sql SELECT * FROM orders ``` -------------------------------- ### Passing Dynamic Placeholder Values via URL Source: https://wpdatatables.com/documentation/table-features/global-search-via-url You can pass values for dynamic placeholders (e.g., VAR1) directly in the URL. This is useful for customizing SQL queries or predefined column filters. ```url ?wdt_var1=your_value ```