We're using the Master Detail extension to show product details in a page template and would like the ability to show/hide certain fields depending on user role. Is there a filter available to call a different template based on role, or a method for wrapping the placeholders in an "if" statement to control what's shown to different users?
This can be done, I will show you an example with our dummy table.
If I have this table:
This is a manual table that has manually entered IDs from wp_users and roles from wp_usermeta:
So, then I go to create an SQL query based table which will display entries based on user roles using this query:
SELECT new_table.id AS ID,
new_table.role AS Role,
new_table.product AS Product,
new_table.amount AS Amount
FROM wp_wpdatatable_6 AS new_table
JOIN wp_usermeta AS new_table_1
ON new_table_1.user_id = %CURRENT_USER_ID%
AND new_table_1.meta_value LIKE CONCAT('%', new_table.role, '%')
When I'm logged in with user ID = 1, I see this:
When I'm logged in with user ID = 4, I see the same thing. And when I'm logged in as one of the subscribers, I can only see the rows where role = subscriber:
The only difference is that as subscriber I don't have access to back-end, so this confirms it is working on both front and back.
So, we're only left with hiding unnecessary columns, adding names, and so on.
I appreciate the response, though it doesn't really address the original question. We're looking for a solution specific to filtering Master Detail page templates - either a way to assign different page templates for different user roles via hook, or a way to wrap the %PLACEHOLDERS% with conditional statements via helper function. Is it possible?
First, you need to create a button (master-detail) column in your existing table with Master-Detail add-on and then set a custom page (post) where users will be redirected after clicking on the button.
Then, on that custom page, you will insert a table created with an SQL query and using placeholders.
If you have that table in the database then you will use it for creating a new table with an SQL query and placeholders.
If you create a manual table then you will use the table that is created in the database (you can find its name in the Editing tab under "MySQL name table for editing").
Those database tables have names like wp_wpdatatable_1,wp_wpdatatable_2... (where wp_ is the default prefix of the database table, but on your end, it could be anything, so look for your_prefix_wpdatatable_1...).
Then, use that table's database name to create a new one with a query.
So you will create a new table like this
SELECT * FROM table_name_from_mysql_name_option WHERE column_name = %VAR1%
Then on that custom page, you would insert some default column_name value in shortcode like
[wpdatatable id=1 var1=test]
Now hooks. You will add this hook in your functions.php of your child theme or theme, depends on what you are using, to filter this var1.
And then you will use our hook for dynamically updating placeholders:
function updateVAR1($tableID){ global $wdtVar1;
// check is set details data from POST, where you'll find all row data
// from MasterDetail after button click in first table
if(isset($_POST['wdt_details_data'])){
$detailsData= json_decode(stripslashes($_POST['wdt_details_data']), true);
// instead of origin_header you will insert value from your column origin header
// on Master-dettails docs you will find Instructions how to create custom
// template (post or page) and below that is shown what is origin header
$wdtVar1 = $detailsData['origin_header'];
}
}
add_action('wpdatatables_before_get_table_metadata', 'updateVAR1');
This is the only way because the shortcode is executed before the content on the page or post.
Like this when the user clicks on the More details button (that you will create with Master-Detail) it will be redirected to your custom page (that you set in MD).
On that page, you will insert the shortcode of the Child table created from the query and placeholder.
After loading the page, the column value will be replaced with a placeholder, placeholders will be replaced in the query and you will get a table that is filtered with that placeholder.
I hope that I haven't confused you with this, but I try to be as detailed as I can so you can understand what and how you can achieve with this.
We're using the Master Detail extension to show product details in a page template and would like the ability to show/hide certain fields depending on user role. Is there a filter available to call a different template based on role, or a method for wrapping the placeholders in an "if" statement to control what's shown to different users?
Hi, Ryan
Thanks for reaching out to us.
This can be done, I will show you an example with our dummy table.
If I have this table:
This is a manual table that has manually entered IDs from wp_users and roles from wp_usermeta:
So, then I go to create an SQL query based table which will display entries based on user roles using this query:
When I'm logged in with user ID = 1, I see this:
When I'm logged in with user ID = 4, I see the same thing. And when I'm logged in as one of the subscribers, I can only see the rows where role = subscriber:
The only difference is that as subscriber I don't have access to back-end, so this confirms it is working on both front and back.
So, we're only left with hiding unnecessary columns, adding names, and so on.
Hope this helps.
Kind Regards,
Miloš Jovanović
[email protected]
Rate my support
Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/
wpDataTables: FAQ | Facebook | Twitter | Instagram | Front-end and back-end demo | Docs
Amelia: FAQ | Facebook | Twitter | Instagram | Amelia demo sites | Docs | Discord Community
You can try wpDataTables add-ons before purchasing on these sandbox sites:
Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables
I appreciate the response, though it doesn't really address the original question. We're looking for a solution specific to filtering Master Detail page templates - either a way to assign different page templates for different user roles via hook, or a way to wrap the %PLACEHOLDERS% with conditional statements via helper function. Is it possible?
Hi, Ryan. Let's try something like this solution.
First, you need to create a button (master-detail) column in your existing table with Master-Detail add-on and then set a custom page (post) where users will be redirected after clicking on the button.
Then, on that custom page, you will insert a table created with an SQL query and using placeholders.
If you have that table in the database then you will use it for creating a new table with an SQL query and placeholders.
If you create a manual table then you will use the table that is created in the database (you can find its name in the Editing tab under "MySQL name table for editing").
Those database tables have names like wp_wpdatatable_1,wp_wpdatatable_2... (where wp_ is the default prefix of the database table, but on your end, it could be anything, so look for your_prefix_wpdatatable_1...).
Then, use that table's database name to create a new one with a query.
So you will create a new table like this
Then on that custom page, you would insert some default column_name value in shortcode like
[wpdatatable id=1 var1=test]
Now hooks. You will add this hook in your functions.php of your child theme or theme, depends on what you are using, to filter this var1.
And then you will use our hook for dynamically updating placeholders:
This is the only way because the shortcode is executed before the content on the page or post.
Like this when the user clicks on the More details button (that you will create with Master-Detail) it will be redirected to your custom page (that you set in MD).
On that page, you will insert the shortcode of the Child table created from the query and placeholder.
After loading the page, the column value will be replaced with a placeholder, placeholders will be replaced in the query and you will get a table that is filtered with that placeholder.
I hope that I haven't confused you with this, but I try to be as detailed as I can so you can understand what and how you can achieve with this.
Let me know if this fits your needs.
Kind Regards,
Miloš Jovanović
[email protected]
Rate my support
Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/
wpDataTables: FAQ | Facebook | Twitter | Instagram | Front-end and back-end demo | Docs
Amelia: FAQ | Facebook | Twitter | Instagram | Amelia demo sites | Docs | Discord Community
You can try wpDataTables add-ons before purchasing on these sandbox sites:
Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables