Posted on by

Giving the User a Way to Delete Their Own Record

I sometimes get requests for providing a way to let users delete their record or provide a way to delete a record from the frontend.

Generally speaking, it’s not a good idea to let the user actually delete a record as I mention below. The safe way to handle this is to give the user a way to flag a record for deletion, and then you can set up your frontend displays such that a record that is flagged for deletion is not visible.

That said, some administrators like the idea of not having to do anything to delete the record, and this tutorial explains how to do that too.

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 Really Delete Their Own Record?

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.

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

  1. ” 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. ”

    Am I missing something obvious? How do you define a new field.. you brushed over that part. The customer fields on my records page only allows for a name and value. Where are you getting all those options?

    1. ‘custom fields’ i mean

    2. Fields are defined on the Manage Database Fields page in the Participants Database plugin menu.

  2. Hi Roland,
    I installed the plugin and it worked as described for some time. Recently I noticed that the message “Your record has been deleted” doesn’t appear no longer after deleting a record. Has something changed?
    Best regards
    Peter

    1. I’m not seeing the problem here, are you aware of any changes to your site that may have affected this? Are you sending them to a different page after they update their record?

      1. Hi Roland,

        it seems to me the problem is related to the “Participants Database Custom Templates” plugin. Not using a custom template will work fine for me, including the one “Your record has been deleted” . Unfortunately I need a different template for activ and passiv members with different data fields.

        Any idea?

        Beste Regards
        Peter

        1. I’d be interested to know what the specific issue is with the Custom Template Location plugin, if there’s a bug, I will want to fix it.

          You don’t need that plugin to use a custom template, you just have to put your custom templates in a different location if you’re not using it. This is in your active theme directory in a folder named “templates”.

        2. You are right, the problem seems to be the custom template itself. Here following you’ll find the custom template I use. It is very simple and will be called with the shortcode [pdb_record template=custom]. Depending on the field “state” the user will see different data.

          ———————-
          participant_values[‘state’];

          /*
          * now, show the record edit form for that type
          */
          switch ( $type ) {
          case ‘aktiv’:
          echo do_shortcode(‘[pdb_record groups="main,personal,aktives_mitglied"]‘);
          break;
          case ‘passiv’:
          echo do_shortcode(‘[pdb_record groups="main,personal"]‘);
          break;
          }
          ———————
          The custom template works fine, except the fact that messages like “Your record has been deleted” or “Your record has been updated” after sending the form will not be displayed. In contrast, using the shortcode [pdb_record] without the custom template parameter, messages will be displayed.

          Regrads, Peter

        3. You need to make sure that there is a valid value for $type when the page is opened after the submission. This template won’t display anything if the $type value does not match one of your cases.

        4. Roland, the custom displays the different fields depending from the field $type. This works fine, but this is not the issue. The problem begins one step later. The issue is that once data has been updated or the user decided to delete the data, after sending the form no confirmation messages like “Your data has been changed” or “Your data has been deleted” will popup.

          Calling the form without the parameter “template=custom” messages will popup. But this is confusing for user because they will see in one or the other case data not relevant for them. Calling the form with [pdb_record template=custom] or [pdb_record] makes the difference. In the first case confirmation messages will not popup while in the second case they do.

          Best regards, Peter

        5. Try handling the default case (i.e. when $type has no value) by showing the [pdb_update_thanks] shortcode.

        6. Hi Roland,

          your proposal does’t solve the issue, quite the contratry, it will generate a couple of sign-up emails, no idea why.

          I changed to a test-system from which I know that it worked in the past. But it runs in the same issue. To exclude a side effect, I activated a wordpress standard template, no improvement. In the next step I deactivated all other plugins, no improvement either.

          Finally I canceled the custom template parameter in the shortcode and everything works fine.

          Now I’m a liitle bit helpless.

          Beste regards, Peter

        7. Try adding this to your switch… it worked for me:

          case null:
            echo do_shortcode('[pdb_record]');

        8. Roland,
          adding

          case null:
          echo do_shortcode(‘[pdb_record]‘);

          to the custom template allways displays all the data fields, but this is not what is intended, depending of the field $state users should see different data fields. It has the same effect as not using the custom template and this is the reason you see messages poping up.

          Regards,
          Peter

        9. You need to make sure that whatever the $state value is, the correct shortcode is showing. The code I provided will only run if the $state value is not defined: that is, when the private ID in the URL is incorrect…which will be the case when the record is deleted.

        10. Dear Roland,

          by chance I found the solution. The problem is related on how somebody does call the record data. It works fine calling the record with “https://site-domain/edit/private_id” and it fails using “https.//site-domain/page-id/?pid=[private_id]”.

          Though in both cases the record form appears, in the first case user messages will popup, in the second case not. No idea why. Changing to the first methode solved my problem.

          Thanks for your help.

          Best regards, Peter

  3. 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.

  4. 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.

  5. 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

  6. 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 Nuria Cancel reply

Your email address will not be published. Required fields are marked *

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

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

  1. ” 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. ”

    Am I missing something obvious? How do you define a new field.. you brushed over that part. The customer fields on my records page only allows for a name and value. Where are you getting all those options?

    1. ‘custom fields’ i mean

    2. Fields are defined on the Manage Database Fields page in the Participants Database plugin menu.

  2. Hi Roland,
    I installed the plugin and it worked as described for some time. Recently I noticed that the message “Your record has been deleted” doesn’t appear no longer after deleting a record. Has something changed?
    Best regards
    Peter

    1. I’m not seeing the problem here, are you aware of any changes to your site that may have affected this? Are you sending them to a different page after they update their record?

      1. Hi Roland,

        it seems to me the problem is related to the “Participants Database Custom Templates” plugin. Not using a custom template will work fine for me, including the one “Your record has been deleted” . Unfortunately I need a different template for activ and passiv members with different data fields.

        Any idea?

        Beste Regards
        Peter

        1. I’d be interested to know what the specific issue is with the Custom Template Location plugin, if there’s a bug, I will want to fix it.

          You don’t need that plugin to use a custom template, you just have to put your custom templates in a different location if you’re not using it. This is in your active theme directory in a folder named “templates”.

        2. You are right, the problem seems to be the custom template itself. Here following you’ll find the custom template I use. It is very simple and will be called with the shortcode [pdb_record template=custom]. Depending on the field “state” the user will see different data.

          ———————-
          participant_values[‘state’];

          /*
          * now, show the record edit form for that type
          */
          switch ( $type ) {
          case ‘aktiv’:
          echo do_shortcode(‘[pdb_record groups="main,personal,aktives_mitglied"]‘);
          break;
          case ‘passiv’:
          echo do_shortcode(‘[pdb_record groups="main,personal"]‘);
          break;
          }
          ———————
          The custom template works fine, except the fact that messages like “Your record has been deleted” or “Your record has been updated” after sending the form will not be displayed. In contrast, using the shortcode [pdb_record] without the custom template parameter, messages will be displayed.

          Regrads, Peter

        3. You need to make sure that there is a valid value for $type when the page is opened after the submission. This template won’t display anything if the $type value does not match one of your cases.

        4. Roland, the custom displays the different fields depending from the field $type. This works fine, but this is not the issue. The problem begins one step later. The issue is that once data has been updated or the user decided to delete the data, after sending the form no confirmation messages like “Your data has been changed” or “Your data has been deleted” will popup.

          Calling the form without the parameter “template=custom” messages will popup. But this is confusing for user because they will see in one or the other case data not relevant for them. Calling the form with [pdb_record template=custom] or [pdb_record] makes the difference. In the first case confirmation messages will not popup while in the second case they do.

          Best regards, Peter

        5. Try handling the default case (i.e. when $type has no value) by showing the [pdb_update_thanks] shortcode.

        6. Hi Roland,

          your proposal does’t solve the issue, quite the contratry, it will generate a couple of sign-up emails, no idea why.

          I changed to a test-system from which I know that it worked in the past. But it runs in the same issue. To exclude a side effect, I activated a wordpress standard template, no improvement. In the next step I deactivated all other plugins, no improvement either.

          Finally I canceled the custom template parameter in the shortcode and everything works fine.

          Now I’m a liitle bit helpless.

          Beste regards, Peter

        7. Try adding this to your switch… it worked for me:

          case null:
            echo do_shortcode('[pdb_record]');

        8. Roland,
          adding

          case null:
          echo do_shortcode(‘[pdb_record]‘);

          to the custom template allways displays all the data fields, but this is not what is intended, depending of the field $state users should see different data fields. It has the same effect as not using the custom template and this is the reason you see messages poping up.

          Regards,
          Peter

        9. You need to make sure that whatever the $state value is, the correct shortcode is showing. The code I provided will only run if the $state value is not defined: that is, when the private ID in the URL is incorrect…which will be the case when the record is deleted.

        10. Dear Roland,

          by chance I found the solution. The problem is related on how somebody does call the record data. It works fine calling the record with “https://site-domain/edit/private_id” and it fails using “https.//site-domain/page-id/?pid=[private_id]”.

          Though in both cases the record form appears, in the first case user messages will popup, in the second case not. No idea why. Changing to the first methode solved my problem.

          Thanks for your help.

          Best regards, Peter

  3. 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.

  4. 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.

  5. 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

  6. 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 Nuria Cancel reply

Your email address will not be published. Required fields are marked *

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