Skip to content

Instantly share code, notes, and snippets.

@siamkreative
Created February 4, 2016 03:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siamkreative/735845931f04a9a44f6b to your computer and use it in GitHub Desktop.
Save siamkreative/735845931f04a9a44f6b to your computer and use it in GitHub Desktop.
Example of customisations for Awesome Support: Adding custom fields and conditional logic
jQuery(document).ready(function ($) {
/**
* Get URL Parameters using jQuery
* http://stackoverflow.com/a/21903119
*/
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
/**
* Hide the title if it is populated via URL parameter
* http://getawesomesupport.dev/submit-ticket/?wpas_title=Example%20ticket%20title
*/
if (getUrlParameter('wpas_title')) {
// Hide the whole form group
$('#wpas_title_wrapper').hide();
// Prevent the JS error "An invalid form control is not focusable"
$('#wpas_title').removeAttr('required');
}
/**
* Conditional Logic
* The functions below will show/hide fields based on answers to other fields
* The change() function ensure that visible state matches initially
*/
$('select[name="wpas_industry"]').on('change', function () {
// Show additional field if the selected option is "Business & Consulting"
var visible = ($(this).val() == 'business') ? true : false;
$('#wpas_industry_business_wrapper').toggle(visible);
// Remove the required attribute when the field is hidden
$('#wpas_industry_business').prop('required', visible);
}).change();
$('input[name="wpas_company_type[]"]').on('change', function () {
// Show additional field if the checkbox "Other" is checked
if (this.value == 'Other') {
$('#wpas_company_type_other_wrapper').toggle(this.checked);
}
}).change();
$('input[name="wpas_redesign_new"]').on('change', function () {
// Show additional field if the radio "Redesign" is checked
var visible = ($(this).val() == 'redesign') ? true : false;
$('#wpas_redesign_url_wrapper').toggle(visible);
}).change();
});
<?php
/*
Plugin Name: AS Customizations
Plugin URI: https://getawesomesupport.com/
Version: 1.0.0
Author: ThemeAvenue
Author URI: http://themeavenue.net
Description: This example plugin shows how to add <a href="https://getawesomesupport.com/documentation/awesome-support/custom-fields/">custom fields</a> and conditional logic to the Ticket Submission form.
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Allow agents/admin to see the forms
add_filter( 'wpas_agent_submit_front_end', '__return_true' );
// Load JavaScript for conditional logic & URL parameters customizations
add_action( 'wp_enqueue_scripts', 'wpas_as_customizations' );
function wpas_as_customizations() {
if (wpas_is_plugin_page()) {
wp_enqueue_script( 'wpas_as_customizations', plugins_url( '/as_customisations.js' , __FILE__ ), array('jquery'), '1.0.0', true );
}
}
// Register all custom fields after the plugin is safely loaded.
add_action( 'plugins_loaded', 'wpas_user_custom_fields' );
function wpas_user_custom_fields() {
wpas_add_custom_field( 'business_name',
array(
'title' => __( 'Name of your business', 'wpas' ),
'field_type' => 'text',
'required' => true
)
);
wpas_add_custom_field( 'industry',
array(
'title' => __( 'Select your industry', 'wpas' ),
'field_type' => 'select',
'required' => true,
'options' => array(
'' => 'Please select',
'accounting' => 'Accounting &amp; Financial',
'agriculture' => 'Agriculture',
'animals' => 'Animal &amp; Pet',
'architectural' => 'Architectural',
'art' => 'Art &amp; Design',
'attorney' => 'Attorney &amp; Law',
'automotive' => 'Automotive',
'bar' => 'Bar &amp; Nightclub',
'business' => 'Business &amp; Consulting',
'children' => 'Childcare',
'cleaning' => 'Cleaning &amp; Maintenance',
'communications' => 'Communications',
'community' => 'Community &amp; Non-Profit',
'computer' => 'Computer',
'construction' => 'Construction',
'cosmetics' => 'Cosmetics &amp; Beauty',
'dating' => 'Dating',
'education' => 'Education',
'entertainment' => 'Entertainment &amp; The Arts',
'environment' => 'Environmental',
'fashion' => 'Fashion',
'floral' => 'Floral',
'food' => 'Food &amp; Drink',
'games' => 'Games &amp; Recreational',
'home' => 'Home Furnishing',
'industrial' => 'Industrial',
'internet' => 'Internet',
'landscaping' => 'Landscaping',
'medical' => 'Medical &amp; Pharmaceutical',
'photography' => 'Photography',
'physical' => 'Physical Fitness',
'politics' => 'Political',
'realestate' => 'Real Estate &amp; Mortgage',
'religious' => 'Religious',
'restaurant' => 'Restaurant',
'retail' => 'Retail',
'security' => 'Security',
'spa' => 'Spa &amp; Esthetics',
'sports' => 'Sport',
'technology' => 'Technology',
'travel' => 'Travel &amp; Hotel',
'wedding' => 'Wedding Service',
)
)
);
wpas_add_custom_field( 'industry_business',
array(
'title' => __( 'Describe what your organization or product does', 'wpas' ),
'field_type' => 'textarea',
'rows' => 3,
'desc' => 'E.g. We sell anvils and other industrial goods to manufacturing companies and hobbyists all over the world.',
'required' => true
)
);
wpas_add_custom_field( 'company_type',
array(
'title' => __( 'Check the box(es) that best describe your company size:', 'wpas' ),
'field_type' => 'checkbox',
'required' => true,
'options' => array(
'Fortune 500 Corporation' => 'Fortune 500 Corporation',
'Non-profit' => 'Non-profit',
'Educational Institution' => 'Educational Institution',
'Startup' => 'Startup',
'Small Business (less than 20 people)' => 'Small Business (less than 20 people)',
'Other' => 'Other'
),
)
);
wpas_add_custom_field( 'company_type_other',
array(
'title' => __( 'Please specify:', 'wpas' ),
'field_type' => 'text'
)
);
wpas_add_custom_field( 'redesign_new',
array(
'title' => __( 'Will this be a redesign of a current site or an entirely new site?', 'wpas' ),
'field_type' => 'radio',
'options' => array(
'redesign' => 'Redesign',
'new_site' => 'New Site'
),
)
);
wpas_add_custom_field( 'redesign_url',
array(
'title' => __( 'Redesign, heh? What’s the URL of the existing site?', 'wpas' ),
'field_type' => 'url',
'placeholder' => 'http://example.com'
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment