Guest User

Garvity forms filter gform_post_data

a guest
May 16th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.87 KB | None | 0 0
  1. <?php
  2. //This filter is executed right before the post is created, allowing post data to be manipulated before the post is created
  3. add_filter("gform_post_data", "set_post_terms", 10, 3);
  4. //post_data is the wordpress array used to create a post, form is the gravity form in the backend, entry is the info submitted by the form to the entries table    
  5. function set_post_terms($post_data, $form, $entry){
  6.  
  7.     //only do this when the form id is 2
  8.     if($form["id"] != 3){
  9.         return $post_data;
  10.     }
  11.     else
  12.     {
  13.        
  14.         //push the new taxonomy college into the existing post_meta tax_input key
  15.         $post_data["post_type"] = 'apprentice';
  16.         $post_data["tax_input"][] = 'college';
  17.         $post_data["tax_input"][] = 'type';
  18.        
  19.         //get values from submitted form (stored in entries)
  20.        
  21.         //interested because
  22.         $interestBecause = $entry["36"];
  23.         if($interestBecause == 'I am / would like to be an Apprentice') $post_data["tax_input"]['type'][] = 'apprentice';
  24.         if($interestBecause == 'I am an Employer') $post_data["tax_input"]['type'][] = 'employer';
  25.        
  26.         //plumpton courses
  27.         $plumpton = array(
  28.             $agriculture = $entry["13.1"],
  29.             $landBasedServiceEngineering  = $entry["13.2"],
  30.             $engineering = $entry["13.3"],
  31.             $horticulture = $entry["13.4"],
  32.             $floristry = $entry["13.5"],
  33.             $treesTimber = $entry["13.6"],
  34.             $environmentalConservation = $entry["13.7"],
  35.             $veterinaryNursing = $entry["13.8"],
  36.             $viticulture = $entry["13.9"],
  37.             $animalCare = $entry["13.11"],
  38.             $horseCare  = $entry["13.12"],
  39.             $foodManufacture = $entry["13.13"],
  40.         );
  41.        
  42.         //sussex downs courses
  43.         $sussexDowns = array(
  44.             $accountancy = $entry["24.1"],
  45.             $businessAdministration = $entry["24.2"],
  46.             $customerService = $entry["24.3"],
  47.             $enterprise = $entry["24.4"],
  48.             $management = $entry["24.5"],
  49.             $teamLeading = $entry["24.6"],
  50.             $sales = $entry["24.7"],
  51.        
  52.             $retail = $entry["25.1"],
  53.             $warehouseStorage = $entry["25.2"],
  54.        
  55.             $brickwork = $entry["26.1"],
  56.             $CarpentryJoinery = $entry["26.2"],
  57.             $maintenanceOperations = $entry["26.3"],
  58.             $paintingDecoration = $entry["26.4"],
  59.             $plumbing = $entry["26.5"],
  60.        
  61.             $cleaningSupportServices = $entry["27.1"],
  62.             $foodBeverageService = $entry["27.2"],
  63.             $foodService = $entry["27.3"],
  64.             $beverageService = $entry["27.4"],
  65.             $frontHouseReception = $entry["27.5"],
  66.             $hairdressing = $entry["27.6"],
  67.             $hospitalityServices = $entry["27.7"],
  68.             $hospitalitySupervisionLeadership = $entry["27.8"],
  69.             $housekeeping = $entry["27.9"],
  70.             $kitchenServices = $entry["27.11"],
  71.             $professionalCookery = $entry["27.12"],
  72.        
  73.             $culturalVenueOperations = $entry["28.1"],
  74.             $liveEventsPromotion = $entry["28.2"],
  75.        
  76.             $childcare = $entry["29.1"],
  77.             $clinicalHealthCareSupport = $entry["29.2"],
  78.             $healthSocialCare = $entry["29.3"],
  79.             $supportingTeachingLearning = $entry["29.4"],
  80.        
  81.             $maintenanceRepair = $entry["30.1"],
  82.             $bodyPaintOperations = $entry["30.2"],
  83.         );
  84.  
  85.         //Check if the form had both plumpton and sussexdowns courses checked
  86.         if( count(array_keys($plumpton, false)) !== count($plumpton) && count(array_keys($sussexDowns, false)) !== count($sussexDowns)  ){
  87.  
  88.             $postCopy = array(
  89.               'comment_status' => $post_data['comment_status'], // 'closed' means no comments.
  90.               'post_author'    => $post_data['post_author'], //The user ID number of the author.
  91.               'post_content'   => $post_data["post_content"], //The full text of the post.
  92.               'post_date_gmt'  => $post_data['post_date_gmt'], //The time post was made, in GMT.
  93.               'post_name'      => $post_data['post_name'], // The name (slug) for your post
  94.               'post_status'    => $post_data['post_status'], //Set the status of the new post.
  95.               'post_title'     => $post_data['post_title'], //The title of your post.
  96.               'post_type'      => 'apprentice', //You may want to insert a regular post, page, link, a menu item or some custom post type
  97.               'tax_input'      => $post_data['tax_input']
  98.             );
  99.            
  100.             $postCopy["tax_input"]['college'][] = 'sussex-downs';
  101.            
  102.             wp_insert_post( $postCopy, $wp_error );
  103.            
  104.             $post_data["tax_input"]['college'][] = 'plumpton';
  105.         }
  106.        
  107.         //count the number of array keys that are false, if this count isn't equal to the number of array values at least one has been ticked
  108.         //plumpton
  109.         elseif( count(array_keys($plumpton, false)) !== count($plumpton) && count(array_keys($sussexDowns, false)) == count($sussexDowns) ){
  110.             $post_data["tax_input"]['college'][] = 'plumpton';
  111.         }
  112.        
  113.         //count the number of array keys that are false, if this count isn't equal to the number of array values at least one has been ticked
  114.         //sussex downs
  115.         elseif( count(array_keys($sussexDowns, false)) !== count($sussexDowns) && count(array_keys($plumpton, false)) == count($plumpton) ){
  116.             $post_data["tax_input"]['college'][] = 'sussex-downs';
  117.         }
  118.      
  119.         return $post_data; //return the changed data to use when the post is created
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment