Last active
February 9, 2017 22:53
-
-
Save JonMasterson/8618341 to your computer and use it in GitHub Desktop.
Populate Gravity Form with email from post meta and output contact form on front end if the post author fills it out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add Contact Meta Box to Posts, Pages, or Any Custom Post Type | |
* Adds a contact form to single post pages when filled out | |
* Get the Gravity Forms plugin, then | |
* put this is your functions file | |
*/ | |
$contact_meta_box = array( | |
'id' => 'jm-contact-meta-box', | |
'title' => 'Contact Email', | |
'context' => 'normal', | |
'priority' => 'high', | |
'fields' => array( | |
array( | |
'name' => 'Email', | |
'id' => 'sh_post_email', | |
'type' => 'text', | |
) | |
) | |
); | |
add_action('admin_menu', 'sh_contact_add_box'); | |
// Add contact meta box to posts | |
function sh_contact_add_box() { | |
global $contact_meta_box; | |
add_meta_box($contact_meta_box['id'], $contact_meta_box['title'], 'show_sh_contact_box', 'page', $contact_meta_box['context'], $contact_meta_box['priority']); | |
add_meta_box($contact_meta_box['id'], $contact_meta_box['title'], 'show_sh_contact_box', 'post', $contact_meta_box['context'], $contact_meta_box['priority']); | |
add_meta_box($contact_meta_box['id'], $contact_meta_box['title'], 'show_sh_contact_box', 'add-your-custom-post-type-slug', $contact_meta_box['context'], $contact_meta_box['priority']); // repeat this line for each CPT you want this to work on | |
} | |
function show_sh_contact_box() { | |
global $contact_meta_box, $post; | |
echo '<input type="hidden" name="sh_contact_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; | |
echo '<table class="form-table">'; | |
foreach ($contact_meta_box['fields'] as $field) { | |
$contact_meta = get_post_meta($post->ID, $field['id'], true); | |
echo '<tr><input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $contact_meta ? $contact_meta : '', '" size="30" style="width:99%" /></tr>'; | |
if ( !isset( $_POST[$field['id']] ) && get_post_meta( $post->ID, $field['id'], true ) == '' ) { | |
echo '<tr><td><span class="description">Enter an email address to add a contact form to this post</span></td></tr>'; | |
} else { | |
echo ''; | |
} | |
} | |
echo '</table>'; | |
} | |
add_action('save_post', 'save_sh_contact_info'); | |
function save_sh_contact_info($post_id) { | |
global $contact_meta_box; | |
if (!isset($_POST['sh_contact_box_nonce']) || !wp_verify_nonce($_POST['sh_contact_box_nonce'], basename(__FILE__))) { | |
return $post_id; | |
} | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
return $post_id; | |
} | |
if ('page' == $_POST['post_type']) { | |
if (!current_user_can('edit_page', $post_id)) { | |
return $post_id; | |
} | |
} elseif (!current_user_can('edit_post', $post_id)) { | |
return $post_id; | |
} | |
foreach ( $contact_meta_box['fields'] as $field ) { | |
$old = get_post_meta( $post_id, $field['id'], true ); | |
$new = $_POST[$field['id']]; | |
if ( $new && $new != $old ) { | |
update_post_meta( $post_id, $field['id'], $new ); | |
} elseif ( '' == $new && $old ) { | |
delete_post_meta( $post_id, $field['id'], $old ); | |
} | |
} | |
} | |
/** | |
* Populate Gravity Form with email from post meta | |
*/ | |
add_filter('gform_field_value_author_email', 'populate_post_author_email'); | |
function populate_post_author_email($value){ | |
global $post; | |
$author_email = get_post_meta($post->ID, 'sh_post_email', true); | |
return $author_email; | |
} | |
/** | |
Read this: | |
http://www.gravityhelp.com/documentation/page/Using_Dynamic_Population | |
http://www.gravityhelp.com/documentation/page/Dynamically_Populating_the_Post_Author | |
Put the following in your single post template, it will output a contact form only if the post author includes an email address in the post. | |
<?php | |
$author_email = get_post_meta($post->ID, 'sh_post_email', true); | |
if ($author_email == '' || !$author_email) : // If email contact, then show the form ?> | |
<?php else : ?> | |
<div class="clrit bot-spc"> | |
<?php gravity_form(<<< enter id number of the form you created with Gravity Forms >>>, $display_title=false, $display_description=false, $display_inactive=false, $field_values=null, $ajax=false, $tabindex); ?> | |
</div> | |
<?php endif; // End if email contact, then show the form ?> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment