Frontend List Record Edit Link
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
In this article, I am providing an example template that you can use. It is very important that you place that template file in the correct location on your website. Please read Using Participants Database Custom Templates for the details.
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. The link to download it is at the bottom of the code window.
Hello Roland,
suddenly the edit link became a text and dosn’t redirect to the edit page.
could you advise.
Thanks,
Check on any changes you made to the plugin configuration, for example you may have changed the configuration of the field that you are using to hold the link. Also, check the “Participant Record Page” setting under the Record Form tab.
Hi Roland,
Thanks for your excellent tutorial.
I have a problem using the information you offer, it seems I’m doing something wrong.
The link does not appear in the editor of participants in the list.
I add the php code in the templates and put the shortcode in the edit field created, but something must be wrong that the link does not appear.
Could you help me please?
I am not very expert in programming.
Thank you
First, make sure the “placeholder” field is correctly defined and named in the custom template. You can check the field on the Manage Database Fields page, make sure you have the name right, also make sure that the field is configured to appear in the list display.
After that, the most likely issue is that the custom template is not found. Take a close look at the Using Participants Database Custom Templates article to make sure you have the name of the template correct and you have placed it in the correct location.
Thank you very much, I review the details you sent me and tell you
I already did what you told me, I hope that correctly and the link still does not appear.
I have a doubt, when creating the “placeholder” field, what form element do I use? text line? Shortcode? Link? etc etc?
You will be using a “placeholder” type field. You have to create it, you cannot change a field’s form element to “Placeholder” it can only be created as a new field.