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?” Often, they are just looking for how to use the plugin as a way to register members who would have special privileges. The plugin wasn’t designed for this kind of thing; it doesn’t have any way of  logging a user in, checking to see if they’ve logged in or giving them certain capabilities.

We don’t have to work too hard to get these kinds of features, though: they are already there, built into WordPress. By using a couple of plugins, you can create a custom user role that works exactly as you need. I usually suggest “User Role Editor” and “Front End Users” to set that up.

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]
global $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]
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.

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.

17 thoughts on “Using Participants Database with WordPress Users

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

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

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

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

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

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

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

17 thoughts on “Using Participants Database with WordPress Users

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

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

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

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

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

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

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