Posted on by

Giving the User a Way to Delete Their Own Record

This is a re-do of a post I made a couple of years ago. The code in that post is outdated and won’t work as expected, so I wanted to provide working code to replace that. Also, with the GDPR regulations, many site managers will want to give users an automatic way to delete their data from the database. In this tutorial, I’ll give you a way to do that.

First Step: Create the Delete Switch

Whether the record is deleted automatically or not, you’ll need the delete switch. I suggest you set this up by defining a new field (maybe call it “Delete Record” as I’ve done in the example) that is a checkbox field with “yes” in the options parameter of the field.

You can maybe put something in the help text that explains that the data will be gone permanently, and they shouldn’t click the box unless they really mean it.

Should You Let Your Users Delete Their Own Records?

The main problem with letting users delete their own record is they may do it unintentionally, and there isn’t any way to recover a deleted record once it’s done. I mean, it wouldn’t be deleted if this was possible, right? So, what I recommend for most sites is a “Delete My Record” switch that doesn’t actually delete the record, but only hides it temporarily until it is deleted by an administrator.

If you want your delete switch to simply hide the record, you need to build that into your lists using a list shortcode filter, for example:

[pdb_list filter="delete_record!=yes"]

Hiding the “deleted” record from single record displays and frontend record edit displays is a bit more complex, you need to use a custom template to prevent such records from showing.

We’re Going to Let Them Do It: Here’s How

I created a simple plugin that you can install that will give your users the ability to actually delete a record. This will be irreversible (unless you want to get it from your backups, big hassle), so make sure your users understand this.

First, of course, you need to set up the delete switch as described above. Then, download and install the plugin below. If you used the same field name for your delete switch as I used above, you won’t need to edit the plugin. There is a message that is shown to the user when they delete their record (line 72), you may want to edit that.

If your delete switch field has a different name, you should change the value of the $delete_switch_field_name property on line 15.

The plugin also fires an event when the record is deleted. The main use for that is to trigger an email to the admin and/or the user to notify them of the deletion. This requires the Email Expansion Kit add-on. If you are a coder, you can use the event for other things you may want to do when this happens.

20 thoughts on “Giving the User a Way to Delete Their Own Record

  1. Hi Roland,

    I installed your plug-in to provide members the oportunity to delete their records by themself. In addition I prepared in the Email extension kit an email in order to confirm the deletion to members. As send action I choosed the “User Record Delete” event for the email. In both, the email and in the user information part I put the [pdb_update_thanks] shortcode.
    While the message in line 72 pops up as expected, the email itself will not be sent. Instead users receive again a registration email as defined in the settings of the PDb Plugin, though their record has been deleted. When omitting the shortcode in the email body the record will be deleted, but beyond that nothing else happens, neither an email will be send nor a message appears.

    Do you have an idea of what I do wrong?

    Regards, Peter

    1. Hi Peter,

      Looks like you’re doing it right.

      However, I forgot to post an update to the tutorial plugin, try reinstalling it from the newly-updated gist.

      https://gist.github.com/xnau/40ff2abd35fb856e3d06dbb4bdbc7169

      1. Roland, thanks for the new version.

        Now, the erroneously sent registration email disappeared. The message pops up as expected, but now as before users will not receive a confirmation email.

        It’s an improvement anyhow!
        Regards, Peter

        1. Did you check that the email is going out? It’s working for me here. Try turning the plugin debugging on, it will log the outgoing email.

        2. Hi Roland,
          it turned out that the confirmation message will be sent to a user only the first time within a day periode. When repeating the test case with the same data a second or a third time a confirmation message will not be sent. Even clearing the cache and all cookies doesn’t have an effect. I assume the issue is within session management.

          As in production environmet the case will not happen, it’s not a problem.

          Thanks & regards
          Peter

        3. Yes, there is a checkbox in the email template that prevents repeat emails within a 24-hour period….this is to prevent flooding.

  2. Does the code above go into the functions.php of the child theme?

    1. It’s a plugin. Take a look at this page for instructions: How to Install a WordPress Plugin from a Gist

      1. I forgot to activate it – thanks for the quick reply!

      2. Hi Roland:

        Is this the new version of the delete switch plugin that you said you will update?

        Thanks: javz

        1. Yes, the gist has been updated and is now current.

        2. Hi Roland:

          I tested your new delete switch plugin, but unfortunately, both the delete and update message does not display. Only blank page.
          Update thanks page = shortcode [ pdb_update_thanks]
          But I think this is the culprit = pdb-update_thanks_message
          Where can I find this file ?
          Your delete switch plugin uses:
          add_filter( ‘pdb-update_thanks_message’, function( $message ) {
          $message = ‘Your record was deleted.’;
          return $message;
          but i think $message is not being read ?
          Because if I don’t use action=”update-thanks”
          Delete message works fine.

          So what do you think? Why is it still displaying a blank page w/o any message?

          Thanks and waiting for your wisdom
          rdgs;
          javz

        3. The page they go to after updating their record must have the [pdb_update_thanks] shortcode.

          If you’re getting a blank page, I suggest you check your php error log for something.

          This code is tested, and working, so we just need to make sure it’s configured correctly.

  3. Mine doesn’t delete. I set up the switch and installed the plugin from the link provided and it didn’t work.

    1. Same issue for me though I kept the name ‘delete_record’…!
      Will not work…

      1. I found the origin of the problem:
        you have to write the ‘yes’ in lower case in the field box because the code strictly says line 44:
        if ( isset( $participant_record[$this->delete_switch_field_name] ) && $participant_record[$this->delete_switch_field_name] === ‘yes’ )
        Could be good to make ‘Yes’ acceptable as well…

        1. Thanks for pointing this out. The instructions specify that the value should be “yes” all lowercase. If you want a different string displayed to the user, you can configure the field value with a title like this: Yes::yes

  4. hello,
    I pasted this in my functions php (child theme) and it broke my website (whoops). I would really still like to have a delete function though! but am reluctant to try pasting it again. what did I do wrong? does it really go into the functions php? I want to allow used to delete their record from the record page.

    1. manage to get it to work, thank you!

      1. What did you do to fix it? Mine won’t work at all.

Leave a Reply to Brad Chalker Cancel reply
You have to agree to the comment policy.

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.

20 thoughts on “Giving the User a Way to Delete Their Own Record

  1. Hi Roland,

    I installed your plug-in to provide members the oportunity to delete their records by themself. In addition I prepared in the Email extension kit an email in order to confirm the deletion to members. As send action I choosed the “User Record Delete” event for the email. In both, the email and in the user information part I put the [pdb_update_thanks] shortcode.
    While the message in line 72 pops up as expected, the email itself will not be sent. Instead users receive again a registration email as defined in the settings of the PDb Plugin, though their record has been deleted. When omitting the shortcode in the email body the record will be deleted, but beyond that nothing else happens, neither an email will be send nor a message appears.

    Do you have an idea of what I do wrong?

    Regards, Peter

    1. Hi Peter,

      Looks like you’re doing it right.

      However, I forgot to post an update to the tutorial plugin, try reinstalling it from the newly-updated gist.

      https://gist.github.com/xnau/40ff2abd35fb856e3d06dbb4bdbc7169

      1. Roland, thanks for the new version.

        Now, the erroneously sent registration email disappeared. The message pops up as expected, but now as before users will not receive a confirmation email.

        It’s an improvement anyhow!
        Regards, Peter

        1. Did you check that the email is going out? It’s working for me here. Try turning the plugin debugging on, it will log the outgoing email.

        2. Hi Roland,
          it turned out that the confirmation message will be sent to a user only the first time within a day periode. When repeating the test case with the same data a second or a third time a confirmation message will not be sent. Even clearing the cache and all cookies doesn’t have an effect. I assume the issue is within session management.

          As in production environmet the case will not happen, it’s not a problem.

          Thanks & regards
          Peter

        3. Yes, there is a checkbox in the email template that prevents repeat emails within a 24-hour period….this is to prevent flooding.

  2. Does the code above go into the functions.php of the child theme?

    1. It’s a plugin. Take a look at this page for instructions: How to Install a WordPress Plugin from a Gist

      1. I forgot to activate it – thanks for the quick reply!

      2. Hi Roland:

        Is this the new version of the delete switch plugin that you said you will update?

        Thanks: javz

        1. Yes, the gist has been updated and is now current.

        2. Hi Roland:

          I tested your new delete switch plugin, but unfortunately, both the delete and update message does not display. Only blank page.
          Update thanks page = shortcode [ pdb_update_thanks]
          But I think this is the culprit = pdb-update_thanks_message
          Where can I find this file ?
          Your delete switch plugin uses:
          add_filter( ‘pdb-update_thanks_message’, function( $message ) {
          $message = ‘Your record was deleted.’;
          return $message;
          but i think $message is not being read ?
          Because if I don’t use action=”update-thanks”
          Delete message works fine.

          So what do you think? Why is it still displaying a blank page w/o any message?

          Thanks and waiting for your wisdom
          rdgs;
          javz

        3. The page they go to after updating their record must have the [pdb_update_thanks] shortcode.

          If you’re getting a blank page, I suggest you check your php error log for something.

          This code is tested, and working, so we just need to make sure it’s configured correctly.

  3. Mine doesn’t delete. I set up the switch and installed the plugin from the link provided and it didn’t work.

    1. Same issue for me though I kept the name ‘delete_record’…!
      Will not work…

      1. I found the origin of the problem:
        you have to write the ‘yes’ in lower case in the field box because the code strictly says line 44:
        if ( isset( $participant_record[$this->delete_switch_field_name] ) && $participant_record[$this->delete_switch_field_name] === ‘yes’ )
        Could be good to make ‘Yes’ acceptable as well…

        1. Thanks for pointing this out. The instructions specify that the value should be “yes” all lowercase. If you want a different string displayed to the user, you can configure the field value with a title like this: Yes::yes

  4. hello,
    I pasted this in my functions php (child theme) and it broke my website (whoops). I would really still like to have a delete function though! but am reluctant to try pasting it again. what did I do wrong? does it really go into the functions php? I want to allow used to delete their record from the record page.

    1. manage to get it to work, thank you!

      1. What did you do to fix it? Mine won’t work at all.

Leave a Reply to Brad Chalker Cancel reply
You have to agree to the comment policy.

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.