Posted on by

Using Participants Database with WordPress Users

A very common support request is along the lines of “how can I give my users a login and password to access their profile?”

There are basically two ways to do this, depending on what privileges you want to give your users. If you just want them to be able to use a couple of pieces of information (email and password, perhaps) to gain access to their Participants Database record for editing (and nothing more than that), you can use the Participant Login add-on to provide a login form for that purpose.

If you need to provide your users with other privileges, such as access to protected areas or the ability to know who they are on other pages, then you have to register your users as WordPress users. That will give them a way to log in and that will be persistent throughout your site.

Participants Database can work very well with WP users, but it can be a little tricky figuring out how the two functionalities will work together. In this post, I lay out a simple scheme that can serve as a starting point for your own integration. What I describe here won’t require much coding, but you’ll probably need to get deeper into the code if you want to take things beyond this.

Relating Records to Users

The easiest part is relating a record to a user. When a user is logged in, several pieces of information about them is available. All we need to do is capture a bit of information that will identify the user and store that with the record. I’ll provide an example of how to do that.

Create a field to hold the user login. Title it “User Login” and the name will end up being “user_login.” Set the form element type to “hidden field.” Set the default to current_user->user_login That will grab the “user_login” property of the current_user object when a new record is created. If your application allows users to create multiple records, they will all be associated with the user through this value. Be sure to set the field to appear in the signup form by checking the “signup” checkbox.

Of course, for this to work, your signup form has to be accessible only to logged-in users.

Showing a List of the User’s Records

It’s not difficult to filter a list display so that it only shows records that relate to a particular user. We need to do this in PHP, but it isn’t too complicated. If you know your way around WordPress templates, you can do this in a template. While that may be the best way to do this, it is a complicated subject on it’s own, so we’ll take a simpler way. You can use PHP directly in your content by installing a plugin: “Insert PHP” is a good, simple choice.

The trick is to use a dynamic value to set the “filter” attribute of the list shortcode. We get the current user’s login and use that value to show only those records that were created by that user. Put this code into the content where you want the list to appear:

[insert_php]
$current_user = wp_get_current_user();
echo do_shortcode('[pdb_list filter="user_login=' . $current_user->user_login . '"]');
[/insert_php]

The way this works is the “do_shortcode” function simply executes the shortcode defined by the string in it’s argument. By assembling that string with a PHP variable, we get a dynamically-generated shortcode. This basic technique can open up a lot of possibilities without doing much coding at all. And, of course, you can use a custom list template with this or any other shortcode attributes you want.

You may want to combine this with: Adding an Edit Record Link to the Frontend List

If the page is visited by a user that is not logged in, no records will be listed, but you may want to put this where only logged-in users will be.

Showing the User an Editable Record

If each user only has one record associated with them, you’ll probably want a way to let them edit it. Showing a single, editable record is a bit trickier because we need to find the record id of the user’s record, but the plugin has methods to help us do that.

Here is the code:

[insert_php]
$current_user = wp_get_current_user();
echo do_shortcode('[pdb_record record_id="' . Participants_Db::get_record_id_by_term('user_login', $current_user->user_login, true) . '"]');
[/insert_php]

The core of this is the call to Participants_Db::get_record_id_by_term() to which we supply the name of the field (or term) to look at and the value to match. The method returns a single id number, if there are multiple matches, the first in the series is returned. (This method can also deliver an array of matching id numbers, if you want to do something with all the records that match. Change the third argument to false.)

The rest is just using that value to select the record to show in the shortcode.

Showing the User their Record

If you just want to show the user their record (not editable) you do the same as above, just using the [pdb_single] shortcode instead.

There is example code here…

Next Steps

You can probably figure out how to show a user’s single record with what I’ve described here. With the right template, that could be something like a user profile or dashboard page.

77 thoughts on “Using Participants Database with WordPress Users

  1. Thank you for your plugin and add-on.
    I want only our members to be a WP user and logged in when managing there own record.
    I have been able to setup the database and registration using the above information and able to get ‘showing the User their Record’ code to work and show my own record, the the basis part should be ok.

    But when I try to use the above example for a user can Edit there record i only receive ‘ No record was found’

    The code used is the below, but I do not really understand what do_shortcode is or where it is defined.

    <?php
    $current_user = wp_get_current_user();
    echo do_shortcode(' No record was found.')
    ;

    Have I overlooked some other code needed to Edit/Update existing Participants database record?

    1. Hi,
      Looks like the php code shown and used is not complete.
      I modified it to include the following and then better, however the output looks awfull with lots of spaces. Anyway to get it to look like the standard [pdb_record] using e-mail links provided og Participant Database login ?

      Her the code which works but output looks awfull

      ID ) {

      $record_id = Participants_Db::get_record_id_by_term(‘user_login’, $current_user->user_login );

      echo do_shortcode( ‘[pdb_record record_id=' . $record_id . ']‘ );

      } else {

      echo ‘You must log in to see your record’;

      }

    2. do_shortcode is a WordPress function and so the details of how that works can be found in the WordPress codex. Basically, what it does is execute whatever shortcode you provide in the argument. So, to show record edit display for a logged in user, you could use code like this:

      <?php
      $current_user = wp_get_current_user();
      $record_id = Participants_Db::get_record_id_by_term( 'user_login', $current_user->user_login );
      echo do_shortcode('[pdb_record record_id=' . $record_id . ']');
      ?>

      you will need to adapt this example code to your specific situation, but that is the general idea.

      1. The issue was solved easy by selecting

        In Danish:

        Fjerne linjeskift Fjerne linjeskift fra alle plugin shortcode output.

        Assume English is something like:

        Remove Newline Remove newline from all plugin shorcode output

  2. Thank you! That will work fine.

  3. Thank you for this wonderful plug-in. It is so easy to use – quite intuitive.

    Noob here – I haven’t been able to find how to differentiate between active and inactive list members. I’d like to be able to hide inactive list members from a general search by the active members but still allow administrators to see, manipulate and export that data. We often have members change status and we don’t want to lose that history by deleting and then re-entering the information later. Can you please point me to the appropriate information to set this up in my own iteration of the plug-in?

    Thanks in advance for the guidance.

    1. Take a look at the docs here: Approving Records you can use the same idea to manage your active/inactive members.

Leave a Reply
You have to agree to the comment policy.

77 thoughts on “Using Participants Database with WordPress Users

  1. Thank you for your plugin and add-on.
    I want only our members to be a WP user and logged in when managing there own record.
    I have been able to setup the database and registration using the above information and able to get ‘showing the User their Record’ code to work and show my own record, the the basis part should be ok.

    But when I try to use the above example for a user can Edit there record i only receive ‘ No record was found’

    The code used is the below, but I do not really understand what do_shortcode is or where it is defined.

    <?php
    $current_user = wp_get_current_user();
    echo do_shortcode(' No record was found.')
    ;

    Have I overlooked some other code needed to Edit/Update existing Participants database record?

    1. Hi,
      Looks like the php code shown and used is not complete.
      I modified it to include the following and then better, however the output looks awfull with lots of spaces. Anyway to get it to look like the standard [pdb_record] using e-mail links provided og Participant Database login ?

      Her the code which works but output looks awfull

      ID ) {

      $record_id = Participants_Db::get_record_id_by_term(‘user_login’, $current_user->user_login );

      echo do_shortcode( ‘[pdb_record record_id=' . $record_id . ']‘ );

      } else {

      echo ‘You must log in to see your record’;

      }

    2. do_shortcode is a WordPress function and so the details of how that works can be found in the WordPress codex. Basically, what it does is execute whatever shortcode you provide in the argument. So, to show record edit display for a logged in user, you could use code like this:

      <?php
      $current_user = wp_get_current_user();
      $record_id = Participants_Db::get_record_id_by_term( 'user_login', $current_user->user_login );
      echo do_shortcode('[pdb_record record_id=' . $record_id . ']');
      ?>

      you will need to adapt this example code to your specific situation, but that is the general idea.

      1. The issue was solved easy by selecting

        In Danish:

        Fjerne linjeskift Fjerne linjeskift fra alle plugin shortcode output.

        Assume English is something like:

        Remove Newline Remove newline from all plugin shorcode output

  2. Thank you! That will work fine.

  3. Thank you for this wonderful plug-in. It is so easy to use – quite intuitive.

    Noob here – I haven’t been able to find how to differentiate between active and inactive list members. I’d like to be able to hide inactive list members from a general search by the active members but still allow administrators to see, manipulate and export that data. We often have members change status and we don’t want to lose that history by deleting and then re-entering the information later. Can you please point me to the appropriate information to set this up in my own iteration of the plug-in?

    Thanks in advance for the guidance.

    1. Take a look at the docs here: Approving Records you can use the same idea to manage your active/inactive members.

Leave a Reply
You have to agree to the comment policy.