I have a custom registration form for my WordPress website, because I didn't want to use the default one and I wanted to add more fields.
I am using ACF where I created a relationship field group called 'related_study_paths' that is filtered using a custom post type called 'studypath'. I have set the location of the field group to "Show this field group if User Form is equal to Register". But since I am not using the default WordPress register form, where it does show up, It, however, does not show up on my custom registration form. How do I make that happen?
I have tried the following code but the ACF fields do not show up. It's just a blank field. How do I fix this and save a related study path for a new user?
Custom Registration Form in functions.php:
<?PHP
// user registration login form
function mepoNET_registration_form() {
// only show the registration form to non-logged-in members
if(!is_user_logged_in()) {
// check if registration is enabled
$registration_enabled = get_option('users_can_register');
// if enabled
if($registration_enabled) {
$output = mepoNET_registration_fields();
} else {
$output = __('User registration is not enabled');
}
return $output;
}
}
add_shortcode('register_form', 'mepoNET_registration_form');
// registration form fields
function mepoNET_registration_fields() {
ob_start(); ?>
<h3 class="mepoNET_reg_header"><?php _e('Register New Account'); ?></h3>
<?php
// show any error messages after form submission
mepoNET_register_messages(); ?>
<form id="mepoNET_registration_form" class="mepoNET_reg_form" action="" method="POST">
<fieldset>
<p class="input_container">
<label for="mepoNET_user_username"><?php _e('Username'); ?></label>
<input name="mepoNET_user_username" id="mepoNET_user_username" class="reg_input_field" type="text"/>
</p>
<p class="input_container">
<label for="mepoNET_user_email"><?php _e('Email'); ?></label>
<input name="mepoNET_user_email" id="mepoNET_user_email" class="reg_input_field" type="email"/>
</p>
<p class="input_container">
<label for="mepoNET_user_first"><?php _e('First Name'); ?></label>
<input name="mepoNET_user_first" id="mepoNET_user_first" type="text" class="reg_input_field" />
</p>
<p class="input_container">
<label for="mepoNET_user_last"><?php _e('Last Name'); ?></label>
<input name="mepoNET_user_last" id="mepoNET_user_last" type="text" class="reg_input_field"/>
</p>
<p class="input_container">
<label for="password"><?php _e('Password'); ?></label>
<input name="mepoNET_user_pass" id="password" class="reg_input_field" type="password"/>
</p>
<p class="input_container">
<label for="password_again"><?php _e('Password Again'); ?></label>
<input name="mepoNET_user_pass_confirm" id="password_again" class="reg_input_field" type="password"/>
</p>
<p class="input_container">
<label for="mepoNET_user_bio"><?php _e('Biographical Description'); ?></label>
<textarea name="mepoNET_user_bio" id="mepoNET_user_bio" class="reg_input_field" oninput="commentResize(this)" rows="5" maxlength="80" style="resize: none; padding: 16px 20px; overflow: hidden;"></textarea>
</p>
<p class="input_container">
<label><?php _e('Select Role:'); ?></label>
<label><input type="checkbox" name="mepoNET_user_role[]" value="student"/> <?php _e('Student'); ?></label>
<label><input type="checkbox" name="mepoNET_user_role[]" value="faculty"/> <?php _e('Faculty'); ?></label>
</p>
<p class="input_container">
<label for="related_study_paths"><?php _e('Related Study Paths'); ?></label>
<?php
// Retrieve the current user's selected study paths
$selected_study_paths = get_field('related_study_paths', 'user_' . get_current_user_id());
// Render the ACF Relationship field choices
acf_form(array(
'post_id' => 'user_' . get_current_user_id(),
'post_title' => false,
'field_groups' => array('group_64c106c91a3dd'), // Replace with your field group key
'fields' => array('related_study_paths'), // Replace with your field name
'return' => false,
'submit_value' => false,
'values' => array('related_study_paths' => $selected_study_paths), // Pass the selected choices
));
if ($selected_study_paths) {
echo '<ul>';
foreach ($selected_study_paths as $study_path) {
echo '<li>' . get_the_title($study_path) . '</li>';
}
echo '</ul>';
}
?>
</p>
<p class="input_container">
<input type="hidden" name="mepoNET_csrf" value="<?php echo wp_create_nonce('mepoNET-csrf'); ?>"/>
<input type="submit" value="<?php _e('Register Your Account'); ?>" class="reg_button"/>
</p>
</fieldset>
</form>
<?php
return ob_get_clean();
}
// register a new user
function mepoNET_add_new_user() {
if (isset( $_POST["mepoNET_user_username"] ) && wp_verify_nonce($_POST['mepoNET_csrf'], 'mepoNET-csrf')) {
$user_login = $_POST["mepoNET_user_username"];
$user_email = $_POST["mepoNET_user_email"];
$user_first = $_POST["mepoNET_user_first"];
$user_last = $_POST["mepoNET_user_last"];
$user_pass = $_POST["mepoNET_user_pass"];
$user_bio = $_POST["mepoNET_user_bio"];
$pass_confirm = $_POST["mepoNET_user_pass_confirm"];
$selected_roles = isset($_POST['mepoNET_user_role']) ? $_POST['mepoNET_user_role'] : array();
// this is required for username checks
require_once(ABSPATH . WPINC . '/registration.php');
$allowed_roles = array('student', 'faculty'); // Define allowed roles
$assigned_role = 'subscriber'; // Default role
foreach ($selected_roles as $role) {
if (in_array($role, $allowed_roles)) {
$assigned_role = $role;
break; // Assign the first valid role and exit the loop
}
}
if(username_exists($user_login)) {
// Username already registered
mepoNET_errors()->add('username_unavailable', __('Username already taken'));
}
if(!validate_username($user_login)) {
// invalid username
mepoNET_errors()->add('username_invalid', __('Invalid username'));
}
if($user_login == '') {
// empty username
mepoNET_errors()->add('username_empty', __('Please enter a username'));
}
if(!is_email($user_email)) {
//invalid email
mepoNET_errors()->add('email_invalid', __('Invalid email'));
}
if(email_exists($user_email)) {
//Email address already registered
mepoNET_errors()->add('email_used', __('Email already registered'));
}
if($user_pass == '') {
// passwords do not match
mepoNET_errors()->add('password_empty', __('Please enter a password'));
}
if($user_pass != $pass_confirm) {
// passwords do not match
mepoNET_errors()->add('password_mismatch', __('Passwords do not match'));
}
$errors = mepoNET_errors()->get_error_messages();
// if no errors then cretate user
if(empty($errors)) {
$new_user_id = wp_insert_user(array(
'user_login' => $user_login,
'user_pass' => $user_pass,
'user_email' => $user_email,
'first_name' => $user_first,
'last_name' => $user_last,
'description' => $user_bio,
'user_registered' => date('Y-m-d H:i:s'),
'role' => $assigned_role,
)
);
if($new_user_id) {
// send an email to the admin
wp_new_user_notification($new_user_id);
// log the new user in
wp_setcookie($user_login, $user_pass, true);
wp_set_current_user($new_user_id, $user_login);
do_action('wp_login', $user_login);
// send the newly created user to the home page after logging them in
wp_redirect(home_url()); exit;
$selected_study_paths = $_POST['acf']['related_study_paths']; // Retrieve the selected study paths
update_field('related_study_paths', $selected_study_paths, 'user_' . $new_user_id);
}
}
}
}
add_action('init', 'mepoNET_add_new_user');
// used for tracking error messages
function mepoNET_errors(){
static $wp_error; // global variable handle
return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null));
}
// displays error messages from form submissions
function mepoNET_register_messages() {
if($codes = mepoNET_errors()->get_error_codes()) {
echo '<div class="mepoNET_errors">';
// Loop error codes and display errors
foreach($codes as $code){
$message = mepoNET_errors()->get_error_message($code);
echo '<span class="error"><strong>' . __('Error') . '</strong>: ' . $message . '</span><br/>';
}
echo '</div>';
}
}
Custom Page Template:
<div class="hidden-header">
<?php acf_form_head(); ?>
<?php get_header();?>
</div>
<div class="register-container">
<div class="entry-logo">
<h1><a href="#"><span>MEPO</span>.NETWORK</a></h1>
</div>
<div class="register-box">
<?php
echo do_shortcode('[register_form]'); ?>
</div>
</div>
Registration Form mepoNET_registration_fields() function):
<p class="input_container">
<label for="mepoNET_user_study_path"><?php _e('Related Study Path'); ?></label>
<select name="mepoNET_user_study_path" id="mepoNET_user_study_path" class="mepoNET_user_study_path">
<option value="">Select Study Path</option>
<?php
// Retrieve options from the 'studypath' custom post type
$study_paths = get_posts(array(
'post_type' => 'studypath',
'posts_per_page' => -1,
));
foreach ($study_paths as $study_path) {
echo '<option value="' . $study_path->ID . '">' . $study_path->post_title . '</option>';
}
?>
</select>
</p>
This code generates a dropdown select element populated with study path options retrieved from the 'studypath' custom post type.
User Registration Handler (mepoNET_add_new_user() function):
$selected_study_path = isset($_POST['mepoNET_user_study_path']) ? $_POST['mepoNET_user_study_path'] : '';
if (!empty($selected_study_path)) {
update_field('related_study_paths', array($selected_study_path), 'user_' . $new_user_id);
}
Here, the selected study path is captured from the form data. If a study path is selected, the update_field() function from the Advanced Custom Fields plugin is used to update the 'related_study_paths' field of the user's meta data.
User Registration Action (add_action('user_register','mepoNET_update_user_study_paths')):
function mepoNET_update_user_study_paths($user_id) {
$selected_study_path = isset($_POST['mepoNET_user_study_path']) ? $_POST['mepoNET_user_study_path'] : '';
if (!empty($selected_study_path)) {
update_field('related_study_paths', array($selected_study_path), 'user_' . $user_id);
}
}
add_action('user_register', 'mepoNET_update_user_study_paths');
This function is hooked to the user_register action, which is triggered when a new user is registered. It captures the selected study path from the form data and updates the 'related_study_paths' field for the newly registered user.