Enabling Users to Add Bright License Keys

Purchasing a Bright license key enables transferable registrations to be made available to a group of the purchasers choice. This document covers how to enable these license keys to be associated with a user's account.

Bright is configured to monitor the values/status of the bright_license user meta variable.

get_user_meta($id, BrightWoocommerceConstants::LICENSE_META, true); 

Bright uses a callback on the WordPress filter update_user_metadata to detect modifications to this field, and update the license keys associated with the users' account accordingly.

The following example involves using the Registration Plus Redux plugin. Using this plugin, we will create a custom field that will allow the user to enter their license key on site registration.

Here's an example of a recommended configuration for Registration Plus Redux:

Configuration of a license key field in Registration Plus Redux
Configuration of a license key field in Registration Plus Redux

This adds a license key field to your WordPress registration page.

With this added, you can generate direct links to the registration page that will automatically prepopulate the license key field. An example link is shown here:

[my-site]/wp-login.php?action=register&bright_licenses=<the license key>

We recommend that you generate this links on the order details in the WooCommerce order-details.php template:

            /* after the customer note in themes/[your-theme]/woocommerce/order/order-details.php */
            if ( BrightWoocommerceIntegration::is_license_order($order)) {
              $license_key = BrightWoocommerceIntegration::get_bright_license_id($order);
              $license_key_url = "/wp-login.php?action=register&bright_licenses={$license_key}";
              $full_url = get_site_url() . $license_key_url;
              ?>
                <tr class="bright-license-key">
                  <th scope="row" title="Use the link to send to your users!!!!">License Key:</th>
              <td><a href="<?php echo $license_key_url; ?>"><?php echo $license_key; ?></a></td>
                </tr>
                <tr class="bright-license-key-link">
                  <th scope="row" title="Use the link to send to your users!!!!">License Key Registration URL:</th>
              <td><a href="<?php echo $full_url; ?>" target="<?php echo $license_key; ?>"><?php echo $full_url ?></a></td>
                </tr>
              <?php
            } 

This will provide the license key, and a shareable direct link to registration with the license key pre-populated:

Order details page with shareable registration link and embedded license key.
Order details page with shareable registration link and embedded license key.

Following this link will take the user to the site registration page, with the license key pre-populated:

Direct linking of a license key field leads to a pre-populated license key during user registration.
Direct linking of a license key field leads to a pre-populated license key during user registration.

NOTE It is very important when using RPR that you use the WP registration form for users who are adding license keys to their Account. While WooCommerce will let you add a registration form to the login page, this form doesn't function correctly in the latest versions of WooCommerce when writing to custom fields. Better to link directly to the real WP registration page from the login page.

In the following example, we will add a custom field to the user's profile to allow them to interact with their Bright license key data:

add_action('show_user_profile', 'bright_show_extra_profile_fields');
add_action('edit_user_profile', 'bright_show_extra_profile_fields');

function bright_show_extra_profile_fields($user) { ?>

  <h3>Course License information</h3>

  <table class="form-table">

  <tr>
  <th><label for="licenses">Licenses</label></th>

  <td>
  <input type="text" name="bright_licenses" id="bright_licenses" value="<?php echo esc_attr(get_the_author_meta('bright_licenses', $user->ID)); ?>" class="regular-text" /><br />
  <span class="description">Please enter your Course Licenses, comma separated.</span>
  </td>
  </tr>

  </table>
  <?php }

add_action('personal_options_update', 'bright_save_extra_profile_fields');
add_action('edit_user_profile_update', 'bright_save_extra_profile_fields');

function bright_save_extra_profile_fields($user_id) {
  if (!current_user_can('edit_user', $user_id))
    return false;

  /* Copy and paste this line for additional fields. Make sure to change 'bright_licenses' to the field ID. */
  update_usermeta($user_id, 'bright_licenses', $_POST['bright_licenses']);
}

IMPORTANT : we don't recommend you have both the Registration Plus Redux (RPR) method AND the WordPress Profile model running on the same site. You'll end up with redundant fields on the profile page, which is very confusing.