Skip to content

How to Add Custom Fields in Submit Ticket

Mayeenul Islam edited this page Aug 21, 2019 · 2 revisions

Users asking for Custom Fields to be enabled when submitting a new ticket. As of NanoSupport v0.6.0 custom fields cannot be added flawlessly. But under certain circumstances, it can be added.

NOTICE
For a more robust solution, we will provide more action and filter hook for easy and flawless customization.

Prerequisites

As of v0.6.0 the requirements are:

  1. You have to have Registration Enabled in your site (WordPress Admin » Settings » General | ✅ Anyone can Register)
  2. You have the embedded Registration form enabled in NanoSupport Settings (NanoSupport » Settings | ✅ Yes, enable Login below ticket submission form)

NOTICE
We will definitely remove the thresholds and make the process easier in upcoming versions, inshALLAH.

WARNING
The Embedded Login feature is a beta feature. Use it at your own risk.

Workaround

You can add the following code in your theme's functions.php to add a Custom Text field below registration form embedded in Submit Ticket page and can save the field's value as a user meta of that particular user.

/**
 * Add Custom Field
 * 
 * No styling is imposed here. It's up to you.
 *
 * @return void
 * --------------------------------------------------------------------------------
 */
function nscustom_add_custom_field_in_submit_ticket() {
    $custom_field = ! empty($_POST['custom_field']) ? trim($_POST['custom_field']) : ''; ?>

    <label for="custom-field"><?php _e( 'Custom Field', 'text-domain' ); ?></label>
    <input type="text" name="custom_field" id="custom-field" class="ns-form-control" value="<?php echo esc_attr( wp_unslash($custom_field) ); ?>" />

<?php
}

add_action('nanosupport_register_form', 'nscustom_add_custom_field_in_submit_ticket'); // 'nanosupport_register_form' hook will only work when embedded registration is enabled.


/**
 * Add validation to the registration process.
 *
 * @param object $errors               WP_Error Object.
 * @param string $sanitized_user_login User's username after it has been sanitized.
 * @param string $user_email           User's email.
 * 
 * @return object
 * --------------------------------------------------------------------------------
 */
function nscustom_registration_process_validation($errors, $sanitized_user_login, $user_email) 
{
    if (isset($_POST['ns_submit']) && (empty($_POST['custom_field']) || ! empty($_POST['custom_field']) && trim($_POST['custom_field']) == '')) {
        $errors->add('custom_field_error', __('<strong>ERROR</strong>: Please enter custom field.', 'text-domain' ));
    }

    return $errors;
}

add_filter('registration_errors', 'nscustom_registration_process_validation', 10, 3);


/**
 * Store Custom Field.
 *
 * @param integer $user_id User ID.
 * 
 * @return void
 * --------------------------------------------------------------------------------
 */
function nscustom_store_custom_field($user_id)
{
    if (isset($_POST['custom_field']) && !empty($_POST['custom_field'])) {
        update_user_meta($user_id, 'custom_field', trim($_POST['custom_field']));
    }
}

add_action('user_register', 'nscustom_store_custom_field'); // 'user_register' hook will only work when embedded registration is enabled.

You can retrieve the additional data of the user like below:

$user_meta = get_user_meta(get_current_user_id(), 'custom_field', true);
echo $user_meta;

More Information