Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created November 10, 2016 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/0042e67f95d829871b2c45792bb27474 to your computer and use it in GitHub Desktop.
Save billerickson/0042e67f95d829871b2c45792bb27474 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+
*/
// Author Form
define( 'EA_AUTHOR_FORM_ID', 5192 );
// Author Field, hidden field in the form above
// In WPForms, set the default value to {query_var key="person"}
define( 'EA_AUTHOR_FIELD_ID', 3 );
/**
* Adds author 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_author_on_form( $form_data ) {
// Only run on the 'Contact an Author' form, and if an author is specified in the URL
if( !( EA_AUTHOR_FORM_ID == $form_data['id'] && isset( $_GET['person'] ) ) )
return;
// Display author
$person = intval( $_GET['person'] );
$name = get_the_author_meta( 'display_name', $person );
$photo = get_avatar( $person, 48 );
echo '<h2 class="author">Contacting: ' . $photo . $name . '</h2>';
}
add_action( 'wpforms_frontend_output', 'ea_author_on_form', 9 );
/**
* Use author 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_author_email_address( $email, $fields, $entry, $form_data ) {
if( !( EA_AUTHOR_FORM_ID == $form_data['id'] ) )
return $email;
$author_id = false;
foreach( $fields as $field ) {
if( EA_AUTHOR_FIELD_ID == $field['id'] ) {
$author_id = intval( $field['value'] );
}
}
if( $author_id ) {
$email['address'] = array( get_the_author_meta( 'email', $author_id ) );
}
return $email;
}
add_filter( 'wpforms_entry_email_atts', 'ea_author_email_address', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment