LMS & eLearning

Add a Custom Field to the LearnDash Registration Form and Save It

Add a company field to the LearnDash registration form, validate it through registration_errors with an error the form can display, and save it as user meta on user_register.

Add a Custom Field to the LearnDash Registration Form and Save It

The LearnDash registration form offers username, email, first name, last name and password, in a configurable order, and nothing else. Sites that need a company name, a staff number or a consent checkbox at sign-up have to add the field themselves. The form is a thin wrapper around the standard WordPress registration flow, which makes this straightforward once you know where it submits. This article adds a required Company field in LearnDash 4.18.0.1, validates it, shows an error in the LearnDash form when it is missing, and saves it as user meta.

How the form submits

learndash_registration_output() in includes/payments/ld-login-registration-functions.php prints a form whose action is wp-login.php?action=register on a single site, or wp-signup.php on multisite. Inside the form it fires learndash_registration_form_fields_before, prints the configured fields, fires learndash_registration_form_fields_after, then adds the LearnDash nonce, a redirect_to field and the ld_register_id of the course or group the learner came from. Because the submission goes to WordPress core, validation happens in the core registration_errors filter and the user is created by register_new_user(), which fires user_register. LearnDash hooks register_post at priority 99 to catch errors and send the visitor back to the LearnDash form with each error code as a query argument, and hooks user_register to save first and last name, set the password and log the user in. Our registration hooks reference lists every hook in that path.

Step 1: print the field

<?php
/**
 * Add a Company field after the LearnDash registration fields.
 */
add_action(
    'learndash_registration_form_fields_after',
    function () {
        $value = isset( $_GET['wwc_company'] ) ? sanitize_text_field( wp_unslash( $_GET['wwc_company'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        ?>
        <p class="learndash-registration-field learndash-registration-field-wwc_company learndash-required">
            <label for="wwc_company"><?php esc_html_e( 'Company', 'wwc' ); ?> <span class="learndash-required-field">*</span></label>
            <input type="text" name="wwc_company" id="wwc_company" value="<?php echo esc_attr( $value ); ?>" aria-required="true" />
        </p>
        <?php
    }
);

The markup copies the classes LearnDash uses for its own fields, so the theme styling applies. The value is read back from the query string because, after a validation error, LearnDash redirects to the form with the submitted built-in fields as query arguments; it does not know about ours, so step 2 adds the value itself.

Step 2: validate and keep the value on error

<?php
/**
 * Require the Company field when the LearnDash registration form was used.
 */
add_filter(
    'registration_errors',
    function ( $errors, $sanitized_user_login, $user_email ) {
        if ( ! isset( $_POST['learndash-registration-form'] ) ) {
            return $errors; // Some other registration form, leave it alone.
        }

        if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['learndash-registration-form'] ) ), 'learndash-registration-form' ) ) {
            return $errors;
        }

        $company = isset( $_POST['wwc_company'] ) ? sanitize_text_field( wp_unslash( $_POST['wwc_company'] ) ) : '';

        if ( '' === $company ) {
            $errors->add( 'wwc_company_required', __( 'Please enter your company name.', 'wwc' ) );
        }

        return $errors;
    },
    10,
    3
);

/**
 * Register the error so the LearnDash form displays it.
 */
add_filter(
    'learndash_registration_errors',
    function ( $conditions ) {
        $conditions['wwc_company_required'] = __( 'Registration requires a company name.', 'wwc' );
        return $conditions;
    }
);

/**
 * Carry the submitted value back to the form after an error.
 */
add_filter(
    'learndash_registration_error_url',
    function ( $redirect_url ) {
        if ( isset( $_POST['wwc_company'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
            $redirect_url = add_query_arg( 'wwc_company', rawurlencode( sanitize_text_field( wp_unslash( $_POST['wwc_company'] ) ) ), $redirect_url ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
        }
        return $redirect_url;
    }
);

Three things are happening. The core registration_errors filter adds an error with a code of our choosing. LearnDash's register_post handler turns every error code into a query argument on the redirect URL and, back on the form, prints an alert for each code that appears in the map returned by learndash_login_error_conditions(), which is why the code must be added to that map through learndash_registration_errors. Finally, learndash_registration_error_url lets us append our field's value so it is not lost.

Step 3: save the value

<?php
/**
 * Save the Company field as user meta.
 */
add_action(
    'user_register',
    function ( $user_id ) {
        if ( ! isset( $_POST['learndash-registration-form'] ) ) {
            return;
        }

        if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['learndash-registration-form'] ) ), 'learndash-registration-form' ) ) {
            return;
        }

        if ( isset( $_POST['wwc_company'] ) ) {
            update_user_meta( $user_id, 'wwc_company', sanitize_text_field( wp_unslash( $_POST['wwc_company'] ) ) );
        }
    },
    20
);

Priority 20 runs after LearnDash's own user_register callback at 10, which does not matter for an unrelated meta key but keeps ordering predictable if you later read LearnDash's ld_register_form meta in the same callback.

Showing the value in the profile

To let administrators see and edit the field, hook show_user_profile and edit_user_profile to print an input, and personal_options_update and edit_user_profile_update to save it with a capability check. The value is also available to certificates through [usermeta field="wwc_company"], which is how a company name usually ends up on a certificate; see the certificate shortcodes guide.

Checkboxes and select fields

A consent checkbox works the same way, with isset( $_POST['wwc_consent'] ) as the validation and a boolean meta value; store the time of consent rather than a bare yes. A select field validates against a fixed list with in_array() using strict comparison. Avoid relying on the HTML required attribute alone; the server-side check is what protects the data.

Multisite

On multisite the form posts to wp-signup.php and the validation path is wpmu_validate_user_signup, with the user created later on activation, so the hooks above do not run in the same way. Treat multisite registration as a separate task.

Making the field conditional

The registration form knows which course or group the visitor came from through the ld_register_id query argument, which is also posted back as a hidden field. Read it inside the field-printing callback with absint( $_GET['ld_register_id'] ?? 0 ) and only print the Company field for courses that need it, for example those in a corporate category. Mirror the same condition in the validation callback using the posted ld_register_id, so a visitor on a consumer course is never blocked by a field they could not see. Because both callbacks derive the decision from the same course ID, the rule stays in one place.

Styling

Reusing the learndash-registration-field class gives the input the theme's spacing and width. For a select or a checkbox, inspect how your theme styles the Remember me checkbox on the login form and copy those classes, since LearnDash itself prints no checkboxes on the registration form. Avoid placeholder-only labels; the visible label the snippet prints is what screen readers announce and what the browser uses for autofill matching.

Testing

  1. Submit the form with Company empty: the LearnDash form should reload showing "Registration requires a company name." and the other fields repopulated.
  2. Submit with a value and a duplicate email: the email error should appear and the Company value should still be in the field.
  3. Register successfully and confirm the user meta exists and the learner is logged in and redirected as before.
  4. Register through the standard wp-login.php form, if it is enabled, and confirm the field is not required there.

When to get help

One field is an hour of work including profile editing. Multi-step registration, fields that feed a CRM, or conditional fields by course are where our LearnDash services team usually helps; a LearnDash developer can also wire the same data into API integrations after sign-up.

Frequently asked questions

Which hook prints extra fields on the LearnDash registration form?

learndash_registration_form_fields_after, or learndash_registration_form_fields_before to place them above the built-in fields.

Why does my error not appear on the form?

LearnDash only prints errors whose code is in the map from learndash_login_error_conditions(). Add your code with the learndash_registration_errors filter.

Where is the value saved?

In user meta on user_register, after the core registration has validated and created the account.

Does this work with the WooCommerce registration form?

No. WooCommerce has its own form and hooks. The snippet checks for the LearnDash nonce and ignores other forms.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.