Advertisement
Guest User

Gravity Form Custom Field Code

a guest
Mar 21st, 2013
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2. // Add a custom field button to the advanced to the field editor
  3. add_filter( 'gform_add_field_buttons', 'ts_add_rec_field' );
  4. function ts_add_rec_field( $field_groups ) {
  5.     foreach( $field_groups as &$group ){
  6.         if( $group["name"] == "advanced_fields" ){ // to add to the Advanced Fields
  7.         //if( $group["name"] == "standard_fields" ){ // to add to the Standard Fields
  8.         //if( $group["name"] == "post_fields" ){ // to add to the Standard Fields
  9.             $group["fields"][] = array(
  10.                 "class"=>"button",
  11.                 "value" => __("Audio Recorder", "gravityforms"),
  12.                 "onclick" => "StartAddField('rec');"
  13.             );
  14.             break;
  15.         }
  16.     }
  17.     return $field_groups;
  18. }
  19.  
  20.  
  21. // Adds title to Audio Recorder custom field
  22. add_filter( 'gform_field_type_title' , 'ts_rec_title' );
  23. function ts_rec_title( $type ) {
  24.     if ( $type == 'rec' )
  25.         return __( 'Audio Recorder' , 'gravityforms' );
  26. }
  27.  
  28.  
  29. // Now we execute some javascript technicalitites for the field to load correctly
  30. add_action( "gform_editor_js", "ts_gform_editor_js" );
  31. function ts_gform_editor_js(){
  32. ?>
  33. <script type='text/javascript'>
  34.     jQuery(document).ready(function($) {
  35.         //this will show all the fields of the HTML field minus a couple that I didn't want to appear.
  36.         fieldSettings["rec"] = ".content_setting, .css_class_setting";
  37.     });
  38.  
  39. </script>
  40. <?php
  41. }
  42.  
  43.  
  44. // add the necessary HTML to make the audio recorder field function
  45.  
  46. add_filter("gform_pre_render", "add_recorder_html");
  47. function add_recorder_html($form) {
  48.     $html_content = "<div><a href='javascript:record()' id='record'>Record</a><a href='javascript:play()' id='play'>Play</a><a href='javascript:stop()' id='stop'>Stop</a><a href='javascript:upload()' id='upload'>Upload (faked)</a></div><span id='time'>0:00</span>";
  49.    
  50.         foreach($form["fields"] as &$field)
  51.         {
  52.             //add the above HTML content if the field type is "rec" (custom Audio Recorder)
  53.             if ($field["type"] == "rec")
  54.             {
  55.                 //set the field content to the html
  56.                 $field["content"] = $html_content;
  57.             }
  58.         }
  59.     //return altered form so changes are displayed
  60.     return $form;      
  61. }
  62.  
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement