Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active December 12, 2017 15:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/e5d8bf5f47e1e6be862dfcaf9cae5df7 to your computer and use it in GitHub Desktop.
Save billerickson/e5d8bf5f47e1e6be862dfcaf9cae5df7 to your computer and use it in GitHub Desktop.
<?php
/**
* Core Functionality Plugin
*
* @package CoreFunctionality
* @since 1.0.0
* @copyright Copyright (c) 2014, Bill Erickson & Jared Atchison
* @license GPL-2.0+
*/
// Trainer Form
define( 'EA_TRAINER_FORM_ID', 160 );
// Hidden field we're using for trainer ID
// In WPForms, set default value to {query_var key="trainer"}
define( 'EA_TRAINER_FIELD_ID', 4 );
/**
* Adds trainer photo and name at top of form
* @author Bill Erickson
* @link http://www.billerickson.net/contact-form-dynamic-notification/
*
* @param array $form_data, form data
*/
function ea_trainer_on_form( $form_data ) {
// Only run on the 'Contact a Trainer' form, and if a trainer is specified in the URL
if( !( EA_TRAINER_FORM_ID == $form_data['id'] && isset( $_GET['trainer'] ) ) )
return;
// Display trainer
$trainer = intval( $_GET['trainer'] );
$name = get_the_title( $trainer );
$photo = get_the_post_thumbnail( $trainer, 'thumbnail', array( 'class' => 'alignleft' ) );
echo '<h2 class="trainer">' . $photo . $name . '</h2>';
}
add_action( 'wpforms_frontend_output', 'ea_trainer_on_form', 9 );
/**
* Use trainer email address
* @author Bill Erickson
* @link http://www.billerickson.net/contact-form-dynamic-notification/
*
* @param array $email, contains all the information used to build email notification (address, message...)
* @param array $fields, form fields
* @param array $entry, form entry
* @param array $form_data, form data
* @return array $email
*/
function ea_trainer_email_address( $email, $fields, $entry, $form_data ) {
if( !( EA_TRAINER_FORM_ID == $form_data['id'] ) )
return $email;
$trainer = false;
foreach( $fields as $field) {
if( EA_TRAINER_FIELD_ID == $field['id'] ) {
$trainer = intval( $field['value'] );
}
}
if( ! $trainer )
return $email;
$trainer_email = is_email( get_post_meta( $trainer, 'ea_trainer_email', true ) );
if( !empty( $trainer_email ) )
$email['address'] = array( $trainer_email );
return $email;
}
add_filter( 'wpforms_entry_email_atts', 'ea_trainer_email_address', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment