Skip to content

Instantly share code, notes, and snippets.

@billerickson billerickson/trainer-contact.php Secret
Last active Dec 12, 2017

Embed
What would you like to do?
<?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
You can’t perform that action at this time.