LMS & eLearning

Snippet: Add a Custom Certificate Field With [usermeta] and a Filter

Print a company name, licence number or any custom user meta on a LearnDash certificate. How [usermeta] resolves fields, when it already works, and the two filters that add, format and protect custom fields.

Snippet: Add a Custom Certificate Field With [usermeta] and a Filter

Certificates often need something that is not a LearnDash value: the learner's company, a professional licence number, a membership ID or a cohort name. Those usually live in user meta, written by a registration plugin or a profile form. LearnDash 4.18.0.1 can print them through the [usermeta] shortcode, but the way that shortcode decides which fields are allowed, and where it reads the value from, is not obvious. This snippet explains the resolution order as the plugin implements it, shows when a custom meta key works with no code, and gives the two filters you need when it does not or when the value needs formatting.

How [usermeta] resolves a field

The shortcode, defined in includes/shortcodes/ld_usermeta.php, takes field (default user_login) and user_id (default the current user). It returns an empty string for guests, loads the user with get_userdata(), and then decides which fields are available:

  • If the viewer is an administrator, or is viewing their own data, the available list is just the requested field, whatever it is.
  • Otherwise the list comes from learndash_get_usermeta_shortcode_available_fields(), which returns ten named fields (user_login, first_name, last_name, first_last_name, display_name, user_nicename, nickname, user_email, user_url, description) through the filter learndash_usermeta_shortcode_available_fields.

If the field is in the list, the value is $userdata->{field}, except first_last_name, which is built from first and last name. Finally the result passes through learndash_usermeta_shortcode_field_value_display with the value, the attributes and the available list.

The detail that matters for custom fields: WP_User has a magic getter, so reading an unknown property such as $userdata->company falls through to get_user_meta() for that key. When the viewer is the learner or an administrator, [usermeta field="company"] therefore already prints the company meta with no code at all.

When it already works on a certificate

Classic certificate PDFs are rendered during a request by the learner, and when an administrator or group leader opens another user's certificate, LearnDash's certificate display logic switches the current user to the learner before rendering. In both cases the viewer and the subject match, so any meta key is allowed. Put this in the certificate content and test it with a real completion:

Issued to [usermeta field="display_name"]
Licence number: [usermeta field="licence_number"]

If the field prints blank, check the meta key name in the database rather than the form label; registration plugins commonly prefix keys. Also confirm the value is a plain string: serialized arrays print as empty because the magic getter returns the single meta value unformatted.

Filter 1: allow the field for other viewers

Outside the certificate, for example in a [usermeta user_id="123"] placed on a group leader's page, a non-administrator viewing someone else gets only the default ten fields. Add yours:

<?php
/**
 * Allow custom user meta keys in [usermeta] for any viewer.
 */
add_filter(
    'learndash_usermeta_shortcode_available_fields',
    function ( $fields, $attr ) {
        $fields['company']        = 'Company';
        $fields['licence_number'] = 'Licence number';
        return $fields;
    },
    10,
    2
);

Only add fields you are happy for group leaders to see. The filter is the plugin's protection against exposing arbitrary meta, which is why it exists; do not return every key.

Filter 2: format or compute the value

The display filter runs for every field, including the built-in ones, so test the field name before changing anything. Three common uses:

<?php
/**
 * Format and compute [usermeta] values for certificates.
 */
add_filter(
    'learndash_usermeta_shortcode_field_value_display',
    function ( $value, $attr, $available ) {
        $user_id = absint( $attr['user_id'] );

        switch ( $attr['field'] ) {
            // Upper-case a licence number and fall back to a dash.
            case 'licence_number':
                $value = strtoupper( trim( (string) $value ) );
                return '' === $value ? '-' : esc_html( $value );

            // A date stored as Y-m-d, printed in the site format.
            case 'date_of_birth':
                $ts = strtotime( (string) $value );
                return $ts ? esc_html( date_i18n( get_option( 'date_format' ), $ts ) ) : '';

            // A virtual field that is not stored at all.
            case 'job_title_company':
                $title   = get_user_meta( $user_id, 'job_title', true );
                $company = get_user_meta( $user_id, 'company', true );
                return esc_html( trim( $title . ', ' . $company, ', ' ) );
        }

        return $value;
    },
    10,
    3
);

For the virtual field to reach the filter when a non-administrator views another user, it also has to be in the available list, so add job_title_company through the first filter as well. On a certificate the learner views themselves, that step is not needed.

Keeping PDF output clean

The classic certificate engine runs do_shortcode() on the content and then hands the HTML to TCPDF, so whatever the filter returns is placed inside your certificate markup. Return plain text or simple inline HTML such as a <strong> tag, and escape anything that came from user input with esc_html(); a stray angle bracket in a company name can otherwise break the surrounding element. If you need a line break, return <br/> rather than a newline, since in legacy TCPDF mode newlines are converted to breaks anyway and would double up. The Certificate Builder runs the same shortcodes inside its paragraph and heading blocks, so these filters apply there too.

Collecting the field in the first place

LearnDash has no profile fields of its own beyond the WordPress user record. The company or licence number has to come from somewhere: the registration form plugin you already use, a WooCommerce checkout field saved to the customer, or a short custom form on the account page that calls update_user_meta(). Whichever you choose, make the key stable and documented, because the certificate depends on it, and consider making the field required at registration for courses whose certificates print it. A blank licence number on a professional certificate is worse than no field at all, which is why the formatting filter above falls back to a dash rather than an empty space.

Testing

Complete a test course with an account that has the meta set, and open the certificate from the course page. Then view the same certificate as an administrator from the learner's profile to confirm the administrator path renders identically. Finally, if you added fields for group leaders, log in as a group leader and check that a field you did not allow still prints empty. The certificate shortcode reference lists the other shortcodes that can sit alongside these values.

When to get help

Values that come from a CRM rather than user meta, certificates that must show data from a membership plugin's own tables, or fields that have to be verified before printing are small integration jobs. They are routine for our LearnDash services team, and a LearnDash developer can wire the source system into these filters, usually in a few hours.

Frequently asked questions

Why does [usermeta field="company"] print blank on my certificate?

Most often the meta key is different from the form label, or the value is stored as an array. Check the key in the usermeta table. The learner viewing their own certificate can read any key, so permissions are rarely the cause.

Can I show a custom field to group leaders without making it public?

Yes. The available-fields filter governs what non-administrators can read about other users. Guests never get output, because the shortcode returns empty when nobody is logged in.

Does this work in the Certificate Builder?

Yes. The builder runs shortcodes inside paragraphs and headings, so [usermeta] and both filters behave the same way; its User Meta block offers the standard fields only.

Can I change a built-in field such as display_name?

Yes. The value filter runs for every field, so you can, for example, title-case the display name. Keep the change scoped with a check on the field name.

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.