prefix . 'abp_staff'; $services_table = $wpdb->prefix . 'appointment_services'; $staff_services = $wpdb->prefix . 'abp_staff_services'; // relationship table $wpdb->query("CREATE TABLE IF NOT EXISTS $staff_services ( id INT AUTO_INCREMENT PRIMARY KEY, staff_id INT NOT NULL, service_id INT NOT NULL, INDEX (staff_id), INDEX (service_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"); function abp_get_all_services() { global $wpdb; return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}appointment_services"); } function abp_get_services_for_staff($staff_id) { global $wpdb; return $wpdb->get_col($wpdb->prepare("SELECT service_id FROM {$wpdb->prefix}abp_staff_services WHERE staff_id = %d", $staff_id)); } if (isset($_POST['abp_add_staff']) || isset($_POST['abp_update_staff'])) { check_admin_referer('abp_add_staff_action', 'abp_add_staff_nonce'); $name = sanitize_text_field($_POST['staff_name']); $email = sanitize_email($_POST['staff_email']); $phone = sanitize_text_field($_POST['staff_phone']); $services = isset($_POST['staff_services']) ? array_map('intval', $_POST['staff_services']) : []; $staff_user = !empty($_POST['staff_user']) ? intval($_POST['staff_user']) : null; if (isset($_POST['staff_id']) && $_POST['staff_id']) { $staff_id = intval($_POST['staff_id']); $wpdb->update($staff_table, [ 'name' => $name, 'email' => $email, 'phone' => $phone, 'user_id' => $staff_user ], ['id' => $staff_id]); $wpdb->delete($staff_services, ['staff_id' => $staff_id]); foreach ($services as $service_id) { $wpdb->insert($staff_services, [ 'staff_id' => $staff_id, 'service_id' => $service_id ]); } echo '

Staff updated successfully.

'; unset($_GET['edit_staff']); $edit_staff = null; $edit_services = []; } else { $wpdb->insert($staff_table, [ 'name' => $name, 'email' => $email, 'phone' => $phone, 'user_id' => $staff_user ]); $staff_id = $wpdb->insert_id; foreach ($services as $service_id) { $wpdb->insert($staff_services, [ 'staff_id' => $staff_id, 'service_id' => $service_id ]); } echo '

Staff added successfully.

'; unset($_GET['edit_staff']); $edit_staff = null; $edit_services = []; } } // Handle delete if (isset($_GET['delete_staff']) && is_numeric($_GET['delete_staff'])) { $staff_id = intval($_GET['delete_staff']); $wpdb->delete($staff_services, ['staff_id' => $staff_id]); $wpdb->delete($staff_table, ['id' => $staff_id]); echo '

Staff deleted successfully.

'; } $edit_staff = null; $edit_services = []; if (isset($_GET['edit_staff']) && is_numeric($_GET['edit_staff'])) { $edit_id = intval($_GET['edit_staff']); $edit_staff = $wpdb->get_row($wpdb->prepare("SELECT * FROM $staff_table WHERE id = %d", $edit_id)); $edit_services = abp_get_services_for_staff($edit_id); } $all_services = abp_get_all_services(); $staff_members = $wpdb->get_results("SELECT * FROM $staff_table ORDER BY id DESC"); ?>

get_col( $wpdb->prepare( "SELECT service_id FROM {$wpdb->prefix}abp_staff_services WHERE staff_id != %d", isset($staff_id) ? $staff_id : 0 ) ); $available_services = array_filter($all_services, function($service) use ($assigned_service_ids, $edit_services) { return !in_array($service->id, $assigned_service_ids) || in_array($service->id, $edit_services); }); ?>

Choose an existing WordPress user with the "Staff" role. (Create user here.)

If service list is empty, please create services first. Only not-assigned services are shown here.

Hold Ctrl (Windows) or Command (Mac) to select multiple options.


All Staff Members

id); $service_names = array_filter(array_map(function ($sid) use ($all_services) { foreach ($all_services as $srv) { if ((int) $srv->id === (int) $sid) return $srv->service_name; } return null; }, $assigned_services)); $user_display = ''; if (!empty($staff->user_id)) { $wp_user = get_userdata($staff->user_id); if ($wp_user) { $user_display = $wp_user->display_name . ' (' . $wp_user->user_email . ')'; } } ?>
ID Name Email Phone WP User Services Created Action
id); ?> name); ?> email); ?> phone); ?> created_at); ?> Edit Delete

No staff members found.