I’m trying to build a manual form on a WordPress page.
My goal is to create a dropdown menu of all the users with a fixed role (in my case “athlete”) and register single users in some sport events.
My code:
<form name="team_captain" method="POST" >
Carnival Name: <input type="text" id="carnival_name" name="carnival_name" />
<input type="submit" name="submit_carnival" value="Create Carnival"/><br />
Event Name: <select name="event_name">
<option value="Ironman Race">Ironman Race</option>
<option value="Board Race">Board Race</option>
<option value="Ski Race">Ski Race</option>
<option value="Surf Race">Surf Race</option>
</select><br />
Athlete Name:
<select name="athlete_name">[insert_php] echo getUsersByRole('athlete','athlete-selector'); [/insert_php]</select><br />
Age Group: <select name="age_group">
<option value="Under 15">Under 15</option>
<option value="Under 17">Under 17</option>
<option value="Under 19">Under 19</option>
<option value="Open">Open</option>
</select><br />
Sex: <input type="radio" name="athlete_sex" value="Male">Male <input type="radio" name="athlete_sex" value="Female">Female<br />
<input type="submit" value="Insert Race"/>
</form>
[insert_php]
if(isset($_POST['submit_carnival'])) {
$carnival_name = $_POST['carnival_name'];
echo "Registrations for ".$carnival_name;
}
elseif(isset($_POST['submit'])) {
$event_name = $_POST["event_name"];
$athlete_sex = $_POST["athlete_sex"];
$age_group = $_POST["age_group"];
$athlete_name = $_POST["athlete_name"];
echo $event_name;
echo $age_group;
echo $athlete_sex;
echo $athtlete_name;
}
[/insert_php]
At this point I can display the name of the carnival, but I can’t display the instances of the athletes. And the dropdown list of the athletes doesn’t work.
This is the function:
function getUsersByRole($role,$name,$selected = '',$extra = '') {
global $wpdb;
$wp_user_search = new WP_User_Query(array("role"=> $role));
$role_data = $wp_user_search->get_results();
foreach($role_data as $item){
$role_data_ids[] = $item->ID;
}
$ids = implode(',', $role_data_ids);
$r = $wpdb->get_results("SELECT * from ".$wpdb->prefix . "users where id IN(".$ids .")", ARRAY_A);
$content .='<select name="'.$name.'" id="'.$name.'" '.$extra.'>';
if($selected == ''){
$content .='<option value="" selected="selected">Choose a user</option>';
}else{
$r_user = $wpdb->get_results("SELECT * from ".$wpdb->prefix . "users where ID = ".$selected."", ARRAY_A);
$content .='<option value="'.$selected.'" selected="selected">'.stripslashes($r_user[0]['display_name']).'</option>';
}
for($i=0; $i<count($r); $i++){
$content .='<option value="'.$r[$i]['ID'].'">'.stripslashes($r[$i]['display_name']).'</option>';
}
$content .='</select>';
return $content;
}
Because you’re looking for a $_POST[“submit”], you’ll have to have an input with the name attribute equal to submit. In your case replace this:
with this:
Your getUsersByRole() function is already including the “select” tag, so replace this line:
with this, notice that I changed “athlete-selector” to “athlete_name”:
With those two changes, things should be working, though I haven’t studied your getUsersByRole() function intensively, so there could be a bug in there. Let me know in a comment below if this works for you or not.