How to add an incremental number in subject for a Ticket #, Entry #, etc.

Posted by on Aug 23, 2013 in How To, Tips

How to add an incremental number in subject for a Ticket #, Entry #, etc.

Add the function code to your theme’s functions.php file or to a custom plugin.
Be aware that if you change your theme later on, the code has to be added to the functions.php of the new theme.

Be sure to set the setting inside the function to control which forms you want this function applied.
example run this code for all forms:
$all_forms = true;

example run this code for form 1 only:

$all_forms = false;
$forms = array('1'); 

example run this code for form 1, 2, and 5 only:

$all_forms = false;
$forms = array('1','2','5');

Adjust the word ‘Ticket #’ as needed.

Code begins now:


function my_action_email_subject($subj, $form_id_num) {
##################################
 // control which forms you want this on
 $all_forms = false; // set to true for process on all forms, or false to use settings below
 $forms = array('1');  // one or more individual forms
 ##################################
 if ( !in_array($form_id_num, $forms) && $all_forms != true)
 return $subj;

  $increment = 1; // default start number
  // set an option for remembering the increment number
  $increment = get_option("subject_number_form_$form_id_num", $increment);

  // increase the number and save it
  update_option("subject_number_form_$form_id_num", $increment+1);

  // uncomment and make a form post if you need to reset this
  // delete_option("subject_number_form_$form_id_num");

  // modify the email subject
  $subj = 'Ticket #'.$increment.' - ' . $subj;

   return $subj;

}
// filter hook for modifying the email subject
add_filter('si_contact_email_subject', 'my_action_email_subject', 1, 2);

OK, maybe you want form 1 to have a Ticket #, and form 2 to have Entry #
Use this function instead:


function my_action_email_subject($subj, $form_id_num) {
##################################
 // control which forms you want this on
 $all_forms = false; // set to true for process on all forms, or false to use settings below
 $forms = array('1','2');  // one or more individual forms
 ##################################
 if ( !in_array($form_id_num, $forms) && $all_forms != true)
 return $subj;

  $increment = 1; // default start number
  // set an option for remembering the increment number
  $increment = get_option("subject_number_form_$form_id_num", $increment);

  // increase the number and save it
  update_option("subject_number_form_$form_id_num", $increment+1);

  // uncomment and make a form post if you need to reset this
  // delete_option("subject_number_form_$form_id_num");

 if ( $form_id_num == 1 ) {
    // modify the email subject
    $subj = 'Ticket #'.$increment.' - ' . $subj;
 }

 if ( $form_id_num == 2 ) {
    // modify the email subject
    $subj = 'Entry #'.$increment.' - ' . $subj;
 }

   return $subj;

}
// filter hook for modifying the email subject
add_filter('si_contact_email_subject', 'my_action_email_subject', 1, 2);

The following examples show how to add an incremental number email field for a Ticket #, Entry #, etc.
Adjust the word ‘Ticket #’ as needed.


function my_action_email_fields($email_fields, $form_id_num) {
##################################
 // control which forms you want this on
 $all_forms = false; // set to true for process on all forms, or false to use settings below
 $forms = array('1');  // one or more individual forms
 ##################################
 if ( !in_array($form_id_num, $forms) && $all_forms != true)
 return $email_fields;

  $increment = 1; // default start number
  // set an option for remembering the increment number
  $increment = get_option("field_number_form_$form_id_num", $increment);

  // increase the number and save it
  update_option("field_number_form_$form_id_num", $increment+1);

  // uncomment and make a form post if you need to reset this
  // delete_option("field_number_form_$form_id_num");

  // make one custom ticket number field
  $email_fields['ticket_number'] = 'Ticket #'.$increment;

   return $email_fields;

}
// filter hook to add any custom fields to email_fields array (not validated)  
add_filter('si_contact_email_fields', 'my_action_email_fields', 1, 2);

Maybe you want form 1 email field to have a Ticket #, and form 2 email field to have Entry #

Use this function instead:


function my_action_email_fields($email_fields, $form_id_num) {
##################################
 // control which forms you want this on
 $all_forms = false; // set to true for process on all forms, or false to use settings below
 $forms = array('1','2');  // one or more individual forms
 ##################################
 if ( !in_array($form_id_num, $forms) && $all_forms != true)
 return $email_fields;

  $increment = 1; // default start number
  // set an option for remembering the increment number
  $increment = get_option("field_number_form_$form_id_num", $increment);

  // increase the number and save it
  update_option("field_number_form_$form_id_num", $increment+1);

  // uncomment and make a form post if you need to reset this
  // delete_option("field_number_form_$form_id_num");

 if ( $form_id_num == 1 ) {
    // make one custom ticket number field
    $email_fields['ticket_number'] = 'Ticket #'.$increment;
 }

 if ( $form_id_num == 2 ) {
    // make one custom entry number field
    $email_fields['entry_number'] = 'Entry #'.$increment;
 }

   return $email_fields;

}
// filter hook to add any custom fields to email_fields array (not validated)  
add_filter('si_contact_email_fields', 'my_action_email_fields', 1, 2);

How to add an incremental number in autoresponder subject for a Ticket #, Entry #, etc.


function my_action_autoresp_email_subject($subj, $form_id_num) {
##################################
 // control which forms you want this on
 $all_forms = false; // set to true for process on all forms, or false to use settings below
 $forms = array('1');  // one or more individual forms
 ##################################
 if ( !in_array($form_id_num, $forms) && $all_forms != true)
 return $subj;

  $increment = 1; // default start number
  // set an option for remembering the increment number
  $increment = get_option("subject_number_form_$form_id_num", $increment);

  // increase the number and save it 
  //(this line IS NOT needed if you also use ticket number in email subject)
  //update_option("subject_number_form_$form_id_num", $increment+1);

  //(this line IS needed if you also use ticket number in email subject)
  $increment = $increment-1;

  // uncomment and make a form post if you need to reset this
  // delete_option("subject_number_form_$form_id_num");

  // modify the email subject
  $subj = 'Ticket #'.$increment.' - ' . $subj;

   return $subj;

}
// filter hook for modifying the email subject
add_filter('si_contact_autoresp_email_subject', 'my_action_autoresp_email_subject', 1, 2);

But maybe you want form 1 to have a Ticket #, and form 2 to have Entry # for autoresponder subject
Use this function instead:


function my_action_autoresp_email_subject($subj, $form_id_num) {
##################################
 // control which forms you want this on
 $all_forms = false; // set to true for process on all forms, or false to use settings below
 $forms = array('1','2');  // one or more individual forms
 ##################################
 if ( !in_array($form_id_num, $forms) && $all_forms != true)
 return $subj;

  $increment = 1; // default start number
  // set an option for remembering the increment number
  $increment = get_option("subject_number_form_$form_id_num", $increment);

  // increase the number and save it 
  //(this line IS NOT needed if you also use ticket number in email subject)
  //update_option("subject_number_form_$form_id_num", $increment+1);

  //(this line IS needed if you also use ticket number in email subject)
  $increment = $increment-1;

  // uncomment and make a form post if you need to reset this
  // delete_option("subject_number_form_$form_id_num");

 if ( $form_id_num == 1 ) {
    // modify the email subject
    $subj = 'Ticket #'.$increment.' - ' . $subj;
 }

 if ( $form_id_num == 2 ) {
    // modify the email subject
    $subj = 'Entry #'.$increment.' - ' . $subj;
 }

   return $subj;

}
// filter hook for modifying the email subject
add_filter('si_contact_autoresp_email_subject', 'my_action_autoresp_email_subject', 1, 2);

Suggested donation for this feature $20

Do you need help?

Send us a Donation:

Donate to Mike Challis