Please Note: We are no longer using forums to facilitate standard support. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Forcing a field to only accept registered usernames of a spesific role as input

  1. Hello
    I have 2 questions.
    1) Is there a way to force a field to only accept registered usernames of a spesific role as input? I could create an enormous multiselect and manually update it every time I get a new user of that role, but I was wondering whether the feild could be updated dynamicly.
    2) Would this field have funcionality problems if there were, say, 1000 users of that role?
    Question 2 is more important to me.
    Thank you

    Posted 3 years ago on Thursday December 22, 2011 | Permalink
  2. You could do this with custom validation using the gform_validation hook. You would populate the list of acceptable usernames (selected from your WordPress database using whatever criteria is important to you) and then compare the submitted username to the list of usernames. If there is no match, return a validation error. If there is a match, continue.

    1000 usernames can be returned from a MySQL database fairly quickly and the array functions in PHP are pretty fast, so I don't think this will be a problem. You'll have to try it out and see and improve it as necessary. For example, instead of pulling all the names of a certain role, you could search the database for a match on the one username that was submitted in the form. No match means they get a validation error. Doing it this way would be no problem at all with 1000 records.

    Posted 3 years ago on Friday December 23, 2011 | Permalink
  3. OK, thank you. :)
    I am new to hooks and custom plugins. So I would like some further advice.
    1) Should I create a plugin for this or should I use my funcions.php?
    2) I am also going to use hooks for other GF forms. If I create a plugin, sould I make one that has all the custom funcionality regardig GF, or sould I make a seperate plugin for each form?

    Posted 3 years ago on Friday December 23, 2011 | Permalink
  4. functions.php is fine in your theme folder. There is little difference between a plugin and a function added to your theme's functions.php.

    You can make it easier on yourself to do this all in your theme's functions.php. No need to copy any GF functionality. Just extend the GF functionality by using the hooks to perform your username validation.

    Posted 3 years ago on Tuesday December 27, 2011 | Permalink
  5. I did something similar to this, verifying a 9 digit code from a file on the server. You could do the same thing using the database instead of a file. And instead of the code, you will verify the username, not the code.

    http://pastebin.com/7ANQh6Rc

    Maybe that will spark some ideas. I adapted that from something I did earlier that was pulling codes from the database, so I can find that for you if you're interested.

    Posted 3 years ago on Tuesday December 27, 2011 | Permalink

 

Support Tickets

Open A New Support Ticket
 Open A New Priority Support Ticket *

* Developer license holders only