Please wait...

Video versionVideo overview of wpDataTables placeholders

wpDataTables placeholdersPlaceholders (variables) definition

wpDataTables Placeholders

Placeholders can be understood as predefined “search and replace” blocks that will be replaced with some actual values that can vary for different pages. Usually this is used for MySQL queries.

Currently wpDataTables has 5 types of placeholders:

  • %CURRENT_POST_ID% – this placeholder will be replaced with the currently active WordPress page or post.
  • %CURRENT_USER_ID% – this placeholder will be replaced with the currently logged in user’s ID (if the user is logged in).
  • %CURRENT_USER_LOGIN% – this placeholder will be replaced with the currently logged in username (if the user is logged in).
  • %VAR1%, %VAR2%, %VAR3% – these are the ‘variable’ placeholders, you can set the defaults for them in the settings, and later override them in the shortcode.
  • %WPDB% –  this placeholder is replaced with the current WordPress’ database table prefix (defaults to “wp_“).

The placeholders configuration block appears when you choose the “MySQL query” table type.

As shown in the screenshot on the right, you can use the placeholders in the query, e.g. you can provide the query like this:

SELECT * FROM my_table
WHERE my_field > %VAR1%
AND my_field < %VAR2%
AND user_id = %CURRENT_USER_ID%

Which means: “show me everything from my_table, where my_field is greater then variable 1, but less then variable 2, and user_id is equal to currently logged in users’s ID”.

Before you try to save the table you need to see if this query actually returns anything. By default %VAR1%, %VAR2% and %VAR3% are equal to empty strings, and %CURRENT_USER_ID% is equal to yours (user that creates the table – typically the Admin user, most probably with ID=1), so if you do not redefine the defaults, this query will be parsed like this on MySQL side:

SELECT * FROM my_table
WHERE my_field > ” 
AND my_field < ” 
AND user_id = 1

wpDataTables Placeholders
wpDataTables Placeholders

Most probably such a query would not return anything, and wpDataTables would say that there’s no data to build the table upon. So to avoid this you would need to expand the placeholder configuration block, and define the default values for the variables: e.g. set %VAR1% equal to 0, and %VAR2% equal to 100. Also you might redefine the value for %CURRENT_USER_ID% which might be needed if the table does not contain any rows for the admin’s ID – e.g. set it to “15“.

The variable values are then saved in the table settings, and will be always used as default if not overriden by shortcode parameters. Only the %CURRENT_USER_ID% is not saved and the provided value is used only at the moment of the table generation, later the actual user ID is always fetched.

So then the query would be parsed like this on MySQL side:

SELECT * FROM my_table
WHERE my_field > 0 
AND my_field < 100 
AND user_id = 15

And if this query returns any data it will successfully create a wpDataTable.

Later you can insert this wpDataTable’s shortcode in your posts or pages, and it will fetch the data for the current user. But also you can override the variable settings; just provide the variable values in the shortcode like this:

[ wpdatatable id=12 var1=150 var2=350]

then the values from shortcode attributes would be applied to the variable and the query would be treated like:

SELECT * FROM my_table
WHERE my_field > 150 
AND my_field < 350 
AND user_id = (current_user_id)

So with using placeholders like this you can use a single wpDataTable to produce many different output tables on different pages.

Variable placeholders are also supported in the “Default value” input in column settings. You can define %VAR1%, %VAR2% or %VAR3% in the “Default value” input of the column, then pass a value of the variable through a shortcode parameter and it will be used as default value for advanced filter on the page, and for the editor input, if the table is editable.

If you are using String for Placeholder ‘variable’ %VAR1% should be surrounded with Single Quote (‘).

wpDataTables Placeholders

Never miss new features!

Join 2000+ newsletter subscribers

Never miss notifications about new cool features, promotions, giveaways or freebies - subscribe to our newsletter! We send about one mail per month, and do our best to keep our announcements interesting.

We never spam or disclose your address to anyone.