Posted on by

Adding an Edit Record Link to the Frontend List

Let’s say you want some users who do not have access to the admin section to be able to edit records. You want to show them a list of records that includes a link to a record edit page.

Set Up the Participant Record Page

First make sure your record edit page (known as the Participant Record Page) is properly set up. This is a page which has the [pdb_record] shortcode in it and will therefore show the user’s editable record when properly accessed. Make sure it’s configured in the plugin settings under the Record Form tab: the “Participant Record Page” setting needs to point to your record edit page.

Setting up the Link

You’ll be showing a list of records with a link to edit a record in each listing, so you’ll need to decide what field the link should go on. For this tutorial, we create a new “placeholder” field (on the Manage Database Fields page) and name it “Edit Link” and give it a default value of “Edit.” (The default value can actually be any text you want shown as the clickable text) This column won’t do anything other than provide clickable text. Go to the Manage List Columns Page to add your edit link to the list display.

It’s important that the field you choose for your edit link is not the same field as you have defined as the “Single Record Link Field” in the settings.

The Custom Template

Now, you’ll need to create a custom template by copying a stock template into your theme folder, in another folder named “templates.” Use the “pdb-list-default.php” file. A detailed look at creating a custom templates is here. Read that article if it is unclear to you how to set up the template.

The custom template example I provide is named pdb-list-edit-link.php To use the template, add the name of the template to the list shortcode like this: [pdb_list template=edit-link]

How the Code Works

Here is the main loop that generates the list as seen in “pdb-list-default.php”:

<tbody>
<?php while ( $this->have_records() ) : $this->the_record(); // each record is one row ?>
  <tr>
    <?php while( $this->have_fields() ) : $this->the_field(); // each field is one cell ?>
      <td class="<?php echo $this->field->name ?>-field">
        <?php $this->field->print_value() ?>
      </td>
  <?php endwhile; // each field ?>
  </tr>
<?php endwhile; // each record ?>
</tbody>

We need to alter the value of a field before it’s displayed, so that goes inside the second loop, before anything is printed.  The plugin has a convenient way to do that: a template helper class with methods that give us easy ways to work with the field values. We set that up by instantiating the Template class object using the current record object ($this).

Here is the code for a template that shows an edit link for each record in the list. This replaces the code shown above.

<tbody>
<?php while ( $this->have_records() ) : $this->the_record(); // each record is one row ?>
    <?php $record = new PDb_Template($this); ?>
  <tr>
    <?php while( $this->have_fields() ) : $this->the_field(); // each field is one cell ?>
      <td class="<?php echo $this->field->name ?>-field">
        <?php 
        /*
         * put the edit link URL into the link property of the field
         */
        if ($this->field->name == 'edit_link') {
          $this->field->link = $record->get_edit_link();
        }
        $this->field->print_value();
        ?>
      </td>
  <?php endwhile; // each field ?>
  </tr>
<?php endwhile; // each record ?>
</tbody>

The way this works, is for each new record, the Template class is instantiated with that record’s data. Then we start looping through the fields in that record. When the “edit_link” field is getting shown, we make the field clickable by putting the edit link URL into the field’s “link” property. The $record->get_edit_link() method is provided by the template object, and makes getting the correct URL of your record edit page easy.

You can use this technique to add a link to any text or image field in the list. To make a field value clickable, set the “link” property of the field ($this->field) to the URL you want the click to go to. If the field already has a link on it, the new link will replace it.

The get_edit_link() property uses the global setting “Participant Record Page” to determine which page the editable record is shown on. You can also set that in the function itself if for some reason you wanted it to be different for this particular template: $record->get_edit_link('record-edit') if you want to send them to a page with the slug “record-edit.”

The Complete Template

Here is the whole template for you to use if you don’t want to create your own.

105 thoughts on “Adding an Edit Record Link to the Frontend List

  1. Hi When I create new “placeholder” field the option to make read only etc does not appear & when I run [pdb_list template=edit-link] the edit link filed shows just text not clickable.

    1. No need to set the placeholder read-only, they are always read-only. That instruction is from before I changed the UI.

      If the field is not clickable, you need to edit the template so that it targets the correct field. On line 42, it looks for the name of the field to put the link on…this must match the field name you are using to show the link.

      Also, of course, you need to have your Participant Record Page set up.

  2. Hi Roland.
    I used your code from above.
    The list page does give me a edit tab that goes to a pid.
    However, when you click on the edit button, it just clears the page I am on.
    Any thoughts why.
    http://r89.134.myftpupload.com/edit-record-link/
    Drop Down go for Section and enter either “M o J”
    Thank you

    1. Works for me… did you fix it?

      1. Seems to work for me now.
        Thanks for getting back to me.
        Any reason why the burial last name is highlighted in blue? If you click on it, it clears the page. Any way to remove color
        Select burial last name and use “anderose”

        1. The reason the last name is a link is in the plugin setting under the list display tab, check the “Single Record Link Field” also “Single Record Page” settings.

          I suggest you scan through the settings and make sure everything is the way you want it.

          The blue is due to your theme CSS applied to all links. If you want to change it, you need to add your own CSS rules that override it.

  3. Hey Roland. I am very confused with this. I have added a new Field Group called Edit Link. The field title is edit link. Form element is Placeholder. Default Value is Edit. The link shows up but does nothing. My page is Edit DB Record with the shortcode [pdb_list template=edit-link]. I copied your template and added it to the templates folder. I added the field to my Manage Lists Columns.

    I can give you access if you would like.

    James

    1. Check the plugin settings under the “Record Form” tab, make sure the “Participant Record Page” setting points to your page with the [pdb_record] shortcode.

      1. Roland – that didn’t work either. When I click the Edit link it reads “No Record Was Found”.

        Can I email you username and password to look at the settings?

        1. Make sure that the shortcode you have on that page is the [pdb_record] shortcode.

        2. Nothing I seem to do will show records to edit. Looking at what @markdtg has accomplished, I am quite jealous.

          I’m just wondering if I can get help making sure the structure is set up correctly. Thanks!

          James

        3. Send me a link to the list display and I’ll take a look. I’m not usually willing to log in to user’s sites.

  4. Hello fellow colleagues,
    I can’t do that with editing.

    I want the logged in user to be able to edit the data, but how? The howto is very confusing, is there no photo spread?

    Best wishes

    1. Take a look at this page:

      Using Participants Database with WordPress Users

      It explains how to link Participants Database to WordPress users.

Leave a 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.

105 thoughts on “Adding an Edit Record Link to the Frontend List

  1. Hi When I create new “placeholder” field the option to make read only etc does not appear & when I run [pdb_list template=edit-link] the edit link filed shows just text not clickable.

    1. No need to set the placeholder read-only, they are always read-only. That instruction is from before I changed the UI.

      If the field is not clickable, you need to edit the template so that it targets the correct field. On line 42, it looks for the name of the field to put the link on…this must match the field name you are using to show the link.

      Also, of course, you need to have your Participant Record Page set up.

  2. Hi Roland.
    I used your code from above.
    The list page does give me a edit tab that goes to a pid.
    However, when you click on the edit button, it just clears the page I am on.
    Any thoughts why.
    http://r89.134.myftpupload.com/edit-record-link/
    Drop Down go for Section and enter either “M o J”
    Thank you

    1. Works for me… did you fix it?

      1. Seems to work for me now.
        Thanks for getting back to me.
        Any reason why the burial last name is highlighted in blue? If you click on it, it clears the page. Any way to remove color
        Select burial last name and use “anderose”

        1. The reason the last name is a link is in the plugin setting under the list display tab, check the “Single Record Link Field” also “Single Record Page” settings.

          I suggest you scan through the settings and make sure everything is the way you want it.

          The blue is due to your theme CSS applied to all links. If you want to change it, you need to add your own CSS rules that override it.

  3. Hey Roland. I am very confused with this. I have added a new Field Group called Edit Link. The field title is edit link. Form element is Placeholder. Default Value is Edit. The link shows up but does nothing. My page is Edit DB Record with the shortcode [pdb_list template=edit-link]. I copied your template and added it to the templates folder. I added the field to my Manage Lists Columns.

    I can give you access if you would like.

    James

    1. Check the plugin settings under the “Record Form” tab, make sure the “Participant Record Page” setting points to your page with the [pdb_record] shortcode.

      1. Roland – that didn’t work either. When I click the Edit link it reads “No Record Was Found”.

        Can I email you username and password to look at the settings?

        1. Make sure that the shortcode you have on that page is the [pdb_record] shortcode.

        2. Nothing I seem to do will show records to edit. Looking at what @markdtg has accomplished, I am quite jealous.

          I’m just wondering if I can get help making sure the structure is set up correctly. Thanks!

          James

        3. Send me a link to the list display and I’ll take a look. I’m not usually willing to log in to user’s sites.

  4. Hello fellow colleagues,
    I can’t do that with editing.

    I want the logged in user to be able to edit the data, but how? The howto is very confusing, is there no photo spread?

    Best wishes

    1. Take a look at this page:

      Using Participants Database with WordPress Users

      It explains how to link Participants Database to WordPress users.

Leave a 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.