Allowing users to edit only their own dataConfiguring wpDataTables as a CRUD interface allowing users to see and edit only their own rows
Many users were asking us for a possibility to allow their front-end users to see and edit only their own data. E.g. own orders or inquiries, own elements added to stock, and similar. The main idea was that users would be able to have full “CRUD” (Create, Read, Update, Delete) functionalities for their entries, but wouldn’t be able neither to see, edit, or delete entries made by others.
Since wpDataTables 1.6 it is an out-of-the box feature – you just need to configure a table in a proper way. You can do it completely from wpDataTables back-end by creating a manual table (or you can create a MySQL query-based table – whatever you prefer) – this tutorial will lead you through the process.
Let’s configure a ‘dummy’ orders table, where each logged in user would be able to see and edit his own orders, but wouldn’t be able to see orders made by other people.
First we would need to open WordPress Admin, go to wpDataTables -> Create table, choose the second option “Create a table manually” and click “Next“:
Then we need to prepare the columns. Let’s say we want the users to be able to enter these fields: order date, product, comment. One more field that we would need to add for such a table would be the user’s ID since wpDataTable needs to identify which row belongs to which user. Then configuration on the next step would be the following:
- First column: Name = “Order date“, type = “Date“
- Second column: Name = “Product“, type = “One-line selectbox“, possible values = “Rectangle, Circle, Square“
- Third column: Name = “Comment“, type = “Multi-line string“
- Fourth column: Name = “User ID“, type = “Integer“
Then click “Create the table” and choose between standard and Excel-like editor
The table is already back-end editable. But not yet front-end editable. To make it editable from the front-end and allow users to edit only their own rows, click the “Editing” tab.
There you need to do the following:
- Enable “Allow editing” switch to enable front-end editing;
- Enable “Users see and edit only their data” switch to enable the features of editing only own rows.
- Select “userid” in the “User ID Column” dropdown.
- Open columns settings and uncheck the “Visible on front-end” checkbox for the “User ID” column since we don’t actually need to see this information in the table.
- Click Apply.
Now, if you insert the table in the post or page, it will look like this:
Add a few rows.
Now if you log out and log in under a different user you will not see these rows. And the other way round: users always have access only to their own records.
So basically, the only difference between usual editable wpDataTables and editable wpDataTables where users can edit only their own rows is the user ID field. If you create a table on MySQL side yourself, just prepare a integer column for storing user_id.