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) over their entries, but wouldn’t be able neither to see, edit, or delete entries made by others.
Since wpDataTables 1.6 this is an out-of-the box feature – you would just need to configure a table in a proper way. You can do it completely from wpDataTables back-end using Table Constructor (or you can create MySQL table manually as before – 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 -> wpDataTable Constructor, choose first option (“I would like to prepare structure and input all the data manually“) and press “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: header = “Order date”, type = “Date”;
- Second column: header = “Product”, type = “One-line selectbox”, possible values = “Rectangle, Circle, Square”;
- Third column: header = “Comment”, type = “Multi-line string”;
- Fourth column: header = “User ID”, type = “Integer”.
Then click “Create the table and open in editor“. You will see the back-end editor with a blank table created by Table Constructor:
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 “Go to table settings” button, you will be redirected to the wpDataTable settings page.
There you need to do the following:
- Click “Front-end editing” checkbox to enable front-end editing;
- Click “Users see and edit only their data” checkbox to enable the features of editing only own rows.
- Select “user_id” in the “User ID Column” dropdown.
- Scroll down to the columns settings and untick the “Visible” checkbox for the “User ID” column since we don’t actually need to see this information in the table.
- Save the table.
Now, if you insert the table in the post or page, it will look like this:
You can enter a couple of lines:
Now if you log out and log in under a different user you will not see this lines:
And the other way round: so 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.