Posted on by

Using Participants Database with WordPress Users

This post was updated November 6, 2019 to be compatible with current versions of WordPress.

A very common support request is along the lines of “how can I give my users access to a set of Participants Database records?”

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 one 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 hidden field named “username” to hold the user login. After creating the field, I changed the title to “User Name” for readability, you should choose whatever title makes sense to you. 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.

example configuration of the field that captures the user login value

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. What we need to do is set up a custom shortcode that filters the list according to the currently logged-in user. What it does is how a list that is filtered by the user_login field using the current user’s login name value. That means that the list will only show records that that user created.

The code included in this tutorial will install a plugin that gives you a shortcode for this purpose: [pdb_user_list].

The tutorial code can be modified for your specific needs, for instance, you may want to combine this with: Adding an Edit Record Link to the Frontend List which you can do by adding a template attribute to the list shortcode string that is used in the user_list_shortcode method in the tutorial code.

The tutorial code provides alternate content if the user is not logged in, you can of course change that to show whatever you want such a user to see. This is also in the user_list_shortcode method.

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.

The plugin provided with this tutorial creates a shortcode for this purpose [pdb_user_record] This shortcode will show the record that matches the user’s ID and allow them to edit it.

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.

Showing the User their Record

If you just want to show the user their record (not editable) there is a shortcode provided for that purpose in the tutorial code. Use [pdb_user_single] to show the user’s record.

The Tutorial Plugin

This plugin will install the three shortcodes described in the article. With a little php knowledge, you can modify the plugin to suit your specific needs.

Be sure to check the private $user_id_field = 'username'; line: that is the name of the field you are using to capture the user’s ID value. It must match the field name you use in your setup.

Installing this plugin: How to Install a WordPress Plugin from a Gist

136 thoughts on “Using Participants Database with WordPress Users

  1. Hi Roland.
    Thanks for your excellent plugin.

    I have an issue filtering the list for the current user.
    On my WP 4.8.1 the code

    [insert_php]
    global $current_user;
    echo do_shortcode(‘[pdb_list filter="user_email=' . $current_user->user_email .'" template=custom1]‘);
    [/insert_php]

    dosen’t work.
    Without filter works properly (show all records, obviously).
    I tried to echo only $current_user->user_email and show the mail address properly
    In DB, the hidden field “user_email” contains the right value.
    I tried to delete “template=custom1” but it doesn’t work.

    Can you help me to solve this problem?
    Thanks in advance.

    Greetings from Italy.

    Fortarrigo

    1. Sometimes you have to use the wp_get_current_user() function instead of the global.

      $current_user = wp_get_current_user();

    2. Having the exact same problem. The filter was working perfectly before. Please assist. Also upgraded to WP 4.8.1.
      Thank you kindly.

      1. I answered this one up the thread… Did you try using the wp_get_current_user() function?

        1. Yes I have tried that…before I wrote the reply to you.
          As I said it has been working nicely for about 4 weeks, filter just not working for the last 5 days and we haven’t touched the site in that time. What can be affecting the filter?

        2. It’s testing fine here. There was an update to the plugin several days ago, but I’m not seeing anything that would indicate the list filter was affected. One quick test you can run is to echo out the shortcode string (without using the do_shortcode) to make sure it’s correct…

          echo '[pdb_list filter="user_login=' . $current_user->user_login . '"]';

        3. Problem seems to lie in the user function called. Please send me the link/instruction to roll back to the previous version of the plugin.

  2. I don’t know why but since the recent updates my user’s record list stopped working!

    global $current_user;
    echo do_shortcode(‘[pdb_list filter="user_login=' . $current_user->user_login . '"]‘);

    I thought it was my php into page plugin that broke, but i tried every single plugin and it isn’t working any longer! Please help!

      1. Awesome! that fixed it, I’ve slightly modified it to list by matching user_email instead so now it looks like this:

        user_email . ‘” template=edit-link]’);
        ?>

        happy it works :D

        1. Hi hoiung.
          My code

          [insert_php]
          global $current_user;
          echo do_shortcode(‘[pdb_list filter="user_email=' . $current_user->user_email . '"]‘);
          [/insert_php]

          doesn’t work for me on WP 4.8.1.
          What’s your whole code?

          Have a nice day.

        2. Hi Fortarrigo, my whole code is:-

          user_email . ‘” template=edit-link]’);
          ?>

  3. Hi,

    Thanks for this great post!
    I’ve used the participants database like you informed above.
    I’ve created a form where the participants can login with their e-mail and number from the participants db.
    After they logged in, they will see the Editable Record.
    Thats good! But now i also like to see who of this participants have been logged in.
    Can you tell me how to set this up?

    1. The plugin does record the time and date that a user updates their record from the frontend in a field called “last_accessed” so that may be of help. Also, there is ah admin notification email that can be sent when someone updates their record from the frontend. This is under the “record form” tab in the plugin settings.

  4. Hi Roland
    Thanks for the wonderful plugin. I am still trying to make it work for my usecase.
    I am using this plugin to capture user preferences. Ideally I just want the logged in user to be able to fill the form, so that its preferences are captures (which I can use somewhere else on my website).

    However the way your plugin works, is that, User need to “signup”, and then go to the main form and fill his preferences. User can also edit the information that he entered (as you explained using… echo do_shortcode(‘[pdb_record record_id="' . Participants_Db::get_record_id_by_term('user_login', $current_user->user_login, true) . '"]‘);

    However the problem is that User is able to again click on the sign up page again, and create a new record, with different preferences, which is confusing.
    If there was a way to allow only signup only once per user, then it will solve my problem. And in that case, each registered user will signup once (where he will only fill his name, and not any other form fields), and then user will be forwarded to Edit record page, where he can fill his prefernces (i have checkboxes), and then afterwards, clicking on signup page should be disabled, or redirect him to edit record page.
    Do you think its doable? Thanks agian.

    Pradeep

    1. First, you can put all those field in the signup form to simplify things, that way there will only be one form for them to fill out.

      Second, if your users are logged in, and you’re recording their ID, you can set up a template that only shows the form if they haven’t filled one out yet.

      You need to do this with some embedded PHP or create a custom shortcode. What it does is get the user’s ID, look for a matching record in Participants Database, then shows the signup form if it doesn’t find the record. For example:

      [insert_php]
      global $current_user;
      $record = Participants_Db::get_record_id_by_term( 'user_login', $current_user->user_login );
      if ( $record === false ) {
      echo do_shortcode( '[pdb_signup]' );
      }
      [/insert_php]

      1. Thanks a lot for the reply. It makes sense. I will try that out. Regards.

  5. Roland, with Participants Database, the data captured on the user goes to the database and the person registered became a Participant. However, what I really want also is this Participant becomes a User on my WordPress. Is there a way to automate it? Otherwise, I would need to manually add to the User section after each registration. I am probably clear as mud but I hope you get what I am after.

    Thanks.
    Ron L
    Toronto

    1. Ron, the plugin does not normally register users, it was designed for situations where that is not needed. (But this could be automated given some custom code, of course.) This article deals with the situation where your already-registered users are accessing Participants Database.

      You may find it easier to have your users register as WP users, then use Participants Database to hold additional information.

      Or, easier still would be any number of “membership” type plugins that register users and give you the ability to add any extra fields you need to their profile. You might not use Participants Database at all in that case.

  6. Would this combination work? The user registers on the site with e.g. Paid Memberships Pro, pays via a gateway and is returned to the ‘thank you’ page which is the signup page for the Participants Database with the user name (and hopefully other fields) pre-populated. They then fill out all the other information in the database. How many, or which fields, can I pre-populate? Name? Email?

    1. I’m sure it’s possible, but not having worked with that plugin, I can’t give you the details. You will need to have some coding skills, and use a custom template for your signup form. In that template, you should be able to grab the user’s values and use them to populate the form fields.

  7. This article is amazing! Could you please help me out a little? I’m having trouble getting the editable list to show. i will display: No record was found. the view only version does show tho. How could i show a sign up form to a new user and and editable form as soon as a record has been saved?

    1. If you have the code set up right and you’re seeing “No Record Was Found” then that means there was no record that has the currently signed-in user’s login name ind the corresponding field of the record. Several things have to be correct in order for this to work, so I can’t say where it’s breaking down, but it is important to understand that the record must have the user’s login name in the record so the shortcode knows which record to load.

      If you have the hidden field correctly configured, then if you set up the signup form to send the user to another page after it’s submitted (the thanks page) and you place the code for the record form on that page, it will open for the user and they can edit it. The user must be logged in to WordPress for that to work.

  8. is it possible to have only the search field without the list and once the search is only have the result ?? I am looking for but can not find the way to do

    1. Yes, you can use the “suppress” attribute:

      [pdb_list search=true suppress=true]

  9. Hi, 10x for this awesome plugin.
    I want to use plugin with registered users, I make user_login field, but I can’t put it in signup form.
    I try [pdb_signup template=bootstrap fields='first_name, email, user_login'] and when I inspect page, I can find field user_login field.
    I can see other hidden fields, but not this.

    1. It is not possible to combine the signup form with a login function. You must have your users log in first if you want registered users to have access to the signup form.

      1. Yes, I know, i have logged user and this user is on page with signup form, but in this from I can’t see user_login field with inspect page tool.

        1. OK, make sure your user_login field is configured to appear in the signup form. It should have the “signup” checkbox checked and it shouldn’t be in an admin group.

  10. The [insert_php] plugin no longer works in recent versions of WordPress.
    I would like to use the plugin with users as described here.

    I have also had problems setting the default value for a filed to be the WordPress username as instructed.

    1. Paul, there are many plugins that you can use to put the PHP code in the the page content: https://wordpress.org/plugins/search.php?q=php+code Try one that is popular and recently updated.

      What happens when you try to set the default value of the field to “current_user->user_login”?

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

136 thoughts on “Using Participants Database with WordPress Users

  1. Hi Roland.
    Thanks for your excellent plugin.

    I have an issue filtering the list for the current user.
    On my WP 4.8.1 the code

    [insert_php]
    global $current_user;
    echo do_shortcode(‘[pdb_list filter="user_email=' . $current_user->user_email .'" template=custom1]‘);
    [/insert_php]

    dosen’t work.
    Without filter works properly (show all records, obviously).
    I tried to echo only $current_user->user_email and show the mail address properly
    In DB, the hidden field “user_email” contains the right value.
    I tried to delete “template=custom1” but it doesn’t work.

    Can you help me to solve this problem?
    Thanks in advance.

    Greetings from Italy.

    Fortarrigo

    1. Sometimes you have to use the wp_get_current_user() function instead of the global.

      $current_user = wp_get_current_user();

    2. Having the exact same problem. The filter was working perfectly before. Please assist. Also upgraded to WP 4.8.1.
      Thank you kindly.

      1. I answered this one up the thread… Did you try using the wp_get_current_user() function?

        1. Yes I have tried that…before I wrote the reply to you.
          As I said it has been working nicely for about 4 weeks, filter just not working for the last 5 days and we haven’t touched the site in that time. What can be affecting the filter?

        2. It’s testing fine here. There was an update to the plugin several days ago, but I’m not seeing anything that would indicate the list filter was affected. One quick test you can run is to echo out the shortcode string (without using the do_shortcode) to make sure it’s correct…

          echo '[pdb_list filter="user_login=' . $current_user->user_login . '"]';

        3. Problem seems to lie in the user function called. Please send me the link/instruction to roll back to the previous version of the plugin.

  2. I don’t know why but since the recent updates my user’s record list stopped working!

    global $current_user;
    echo do_shortcode(‘[pdb_list filter="user_login=' . $current_user->user_login . '"]‘);

    I thought it was my php into page plugin that broke, but i tried every single plugin and it isn’t working any longer! Please help!

      1. Awesome! that fixed it, I’ve slightly modified it to list by matching user_email instead so now it looks like this:

        user_email . ‘” template=edit-link]’);
        ?>

        happy it works :D

        1. Hi hoiung.
          My code

          [insert_php]
          global $current_user;
          echo do_shortcode(‘[pdb_list filter="user_email=' . $current_user->user_email . '"]‘);
          [/insert_php]

          doesn’t work for me on WP 4.8.1.
          What’s your whole code?

          Have a nice day.

        2. Hi Fortarrigo, my whole code is:-

          user_email . ‘” template=edit-link]’);
          ?>

  3. Hi,

    Thanks for this great post!
    I’ve used the participants database like you informed above.
    I’ve created a form where the participants can login with their e-mail and number from the participants db.
    After they logged in, they will see the Editable Record.
    Thats good! But now i also like to see who of this participants have been logged in.
    Can you tell me how to set this up?

    1. The plugin does record the time and date that a user updates their record from the frontend in a field called “last_accessed” so that may be of help. Also, there is ah admin notification email that can be sent when someone updates their record from the frontend. This is under the “record form” tab in the plugin settings.

  4. Hi Roland
    Thanks for the wonderful plugin. I am still trying to make it work for my usecase.
    I am using this plugin to capture user preferences. Ideally I just want the logged in user to be able to fill the form, so that its preferences are captures (which I can use somewhere else on my website).

    However the way your plugin works, is that, User need to “signup”, and then go to the main form and fill his preferences. User can also edit the information that he entered (as you explained using… echo do_shortcode(‘[pdb_record record_id="' . Participants_Db::get_record_id_by_term('user_login', $current_user->user_login, true) . '"]‘);

    However the problem is that User is able to again click on the sign up page again, and create a new record, with different preferences, which is confusing.
    If there was a way to allow only signup only once per user, then it will solve my problem. And in that case, each registered user will signup once (where he will only fill his name, and not any other form fields), and then user will be forwarded to Edit record page, where he can fill his prefernces (i have checkboxes), and then afterwards, clicking on signup page should be disabled, or redirect him to edit record page.
    Do you think its doable? Thanks agian.

    Pradeep

    1. First, you can put all those field in the signup form to simplify things, that way there will only be one form for them to fill out.

      Second, if your users are logged in, and you’re recording their ID, you can set up a template that only shows the form if they haven’t filled one out yet.

      You need to do this with some embedded PHP or create a custom shortcode. What it does is get the user’s ID, look for a matching record in Participants Database, then shows the signup form if it doesn’t find the record. For example:

      [insert_php]
      global $current_user;
      $record = Participants_Db::get_record_id_by_term( 'user_login', $current_user->user_login );
      if ( $record === false ) {
      echo do_shortcode( '[pdb_signup]' );
      }
      [/insert_php]

      1. Thanks a lot for the reply. It makes sense. I will try that out. Regards.

  5. Roland, with Participants Database, the data captured on the user goes to the database and the person registered became a Participant. However, what I really want also is this Participant becomes a User on my WordPress. Is there a way to automate it? Otherwise, I would need to manually add to the User section after each registration. I am probably clear as mud but I hope you get what I am after.

    Thanks.
    Ron L
    Toronto

    1. Ron, the plugin does not normally register users, it was designed for situations where that is not needed. (But this could be automated given some custom code, of course.) This article deals with the situation where your already-registered users are accessing Participants Database.

      You may find it easier to have your users register as WP users, then use Participants Database to hold additional information.

      Or, easier still would be any number of “membership” type plugins that register users and give you the ability to add any extra fields you need to their profile. You might not use Participants Database at all in that case.

  6. Would this combination work? The user registers on the site with e.g. Paid Memberships Pro, pays via a gateway and is returned to the ‘thank you’ page which is the signup page for the Participants Database with the user name (and hopefully other fields) pre-populated. They then fill out all the other information in the database. How many, or which fields, can I pre-populate? Name? Email?

    1. I’m sure it’s possible, but not having worked with that plugin, I can’t give you the details. You will need to have some coding skills, and use a custom template for your signup form. In that template, you should be able to grab the user’s values and use them to populate the form fields.

  7. This article is amazing! Could you please help me out a little? I’m having trouble getting the editable list to show. i will display: No record was found. the view only version does show tho. How could i show a sign up form to a new user and and editable form as soon as a record has been saved?

    1. If you have the code set up right and you’re seeing “No Record Was Found” then that means there was no record that has the currently signed-in user’s login name ind the corresponding field of the record. Several things have to be correct in order for this to work, so I can’t say where it’s breaking down, but it is important to understand that the record must have the user’s login name in the record so the shortcode knows which record to load.

      If you have the hidden field correctly configured, then if you set up the signup form to send the user to another page after it’s submitted (the thanks page) and you place the code for the record form on that page, it will open for the user and they can edit it. The user must be logged in to WordPress for that to work.

  8. is it possible to have only the search field without the list and once the search is only have the result ?? I am looking for but can not find the way to do

    1. Yes, you can use the “suppress” attribute:

      [pdb_list search=true suppress=true]

  9. Hi, 10x for this awesome plugin.
    I want to use plugin with registered users, I make user_login field, but I can’t put it in signup form.
    I try [pdb_signup template=bootstrap fields='first_name, email, user_login'] and when I inspect page, I can find field user_login field.
    I can see other hidden fields, but not this.

    1. It is not possible to combine the signup form with a login function. You must have your users log in first if you want registered users to have access to the signup form.

      1. Yes, I know, i have logged user and this user is on page with signup form, but in this from I can’t see user_login field with inspect page tool.

        1. OK, make sure your user_login field is configured to appear in the signup form. It should have the “signup” checkbox checked and it shouldn’t be in an admin group.

  10. The [insert_php] plugin no longer works in recent versions of WordPress.
    I would like to use the plugin with users as described here.

    I have also had problems setting the default value for a filed to be the WordPress username as instructed.

    1. Paul, there are many plugins that you can use to put the PHP code in the the page content: https://wordpress.org/plugins/search.php?q=php+code Try one that is popular and recently updated.

      What happens when you try to set the default value of the field to “current_user->user_login”?

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