Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //This filter is executed right before the post is created, allowing post data to be manipulated before the post is created
- add_filter("gform_post_data", "set_post_terms", 10, 3);
- //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
- function set_post_terms($post_data, $form, $entry){
- //only do this when the form id is 2
- if($form["id"] != 3){
- return $post_data;
- }
- else
- {
- //push the new taxonomy college into the existing post_meta tax_input key
- $post_data["post_type"] = 'apprentice';
- $post_data["tax_input"][] = 'college';
- $post_data["tax_input"][] = 'type';
- //get values from submitted form (stored in entries)
- //interested because
- $interestBecause = $entry["36"];
- if($interestBecause == 'I am / would like to be an Apprentice') $post_data["tax_input"]['type'][] = 'apprentice';
- if($interestBecause == 'I am an Employer') $post_data["tax_input"]['type'][] = 'employer';
- //plumpton courses
- $plumpton = array(
- $agriculture = $entry["13.1"],
- $landBasedServiceEngineering = $entry["13.2"],
- $engineering = $entry["13.3"],
- $horticulture = $entry["13.4"],
- $floristry = $entry["13.5"],
- $treesTimber = $entry["13.6"],
- $environmentalConservation = $entry["13.7"],
- $veterinaryNursing = $entry["13.8"],
- $viticulture = $entry["13.9"],
- $animalCare = $entry["13.11"],
- $horseCare = $entry["13.12"],
- $foodManufacture = $entry["13.13"],
- );
- //sussex downs courses
- $sussexDowns = array(
- $accountancy = $entry["24.1"],
- $businessAdministration = $entry["24.2"],
- $customerService = $entry["24.3"],
- $enterprise = $entry["24.4"],
- $management = $entry["24.5"],
- $teamLeading = $entry["24.6"],
- $sales = $entry["24.7"],
- $retail = $entry["25.1"],
- $warehouseStorage = $entry["25.2"],
- $brickwork = $entry["26.1"],
- $CarpentryJoinery = $entry["26.2"],
- $maintenanceOperations = $entry["26.3"],
- $paintingDecoration = $entry["26.4"],
- $plumbing = $entry["26.5"],
- $cleaningSupportServices = $entry["27.1"],
- $foodBeverageService = $entry["27.2"],
- $foodService = $entry["27.3"],
- $beverageService = $entry["27.4"],
- $frontHouseReception = $entry["27.5"],
- $hairdressing = $entry["27.6"],
- $hospitalityServices = $entry["27.7"],
- $hospitalitySupervisionLeadership = $entry["27.8"],
- $housekeeping = $entry["27.9"],
- $kitchenServices = $entry["27.11"],
- $professionalCookery = $entry["27.12"],
- $culturalVenueOperations = $entry["28.1"],
- $liveEventsPromotion = $entry["28.2"],
- $childcare = $entry["29.1"],
- $clinicalHealthCareSupport = $entry["29.2"],
- $healthSocialCare = $entry["29.3"],
- $supportingTeachingLearning = $entry["29.4"],
- $maintenanceRepair = $entry["30.1"],
- $bodyPaintOperations = $entry["30.2"],
- );
- //Check if the form had both plumpton and sussexdowns courses checked
- if( count(array_keys($plumpton, false)) !== count($plumpton) && count(array_keys($sussexDowns, false)) !== count($sussexDowns) ){
- $postCopy = array(
- 'comment_status' => $post_data['comment_status'], // 'closed' means no comments.
- 'post_author' => $post_data['post_author'], //The user ID number of the author.
- 'post_content' => $post_data["post_content"], //The full text of the post.
- 'post_date_gmt' => $post_data['post_date_gmt'], //The time post was made, in GMT.
- 'post_name' => $post_data['post_name'], // The name (slug) for your post
- 'post_status' => $post_data['post_status'], //Set the status of the new post.
- 'post_title' => $post_data['post_title'], //The title of your post.
- 'post_type' => 'apprentice', //You may want to insert a regular post, page, link, a menu item or some custom post type
- 'tax_input' => $post_data['tax_input']
- );
- $postCopy["tax_input"]['college'][] = 'sussex-downs';
- wp_insert_post( $postCopy, $wp_error );
- $post_data["tax_input"]['college'][] = 'plumpton';
- }
- //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
- //plumpton
- elseif( count(array_keys($plumpton, false)) !== count($plumpton) && count(array_keys($sussexDowns, false)) == count($sussexDowns) ){
- $post_data["tax_input"]['college'][] = 'plumpton';
- }
- //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
- //sussex downs
- elseif( count(array_keys($sussexDowns, false)) !== count($sussexDowns) && count(array_keys($plumpton, false)) == count($plumpton) ){
- $post_data["tax_input"]['college'][] = 'sussex-downs';
- }
- return $post_data; //return the changed data to use when the post is created
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment