Skip to content

Instantly share code, notes, and snippets.

@hearvox
Last active March 16, 2018 14:19
Show Gist options
  • Save hearvox/d0af7a341200b4d43aa5c428cc98fd93 to your computer and use it in GitHub Desktop.
Save hearvox/d0af7a341200b4d43aa5c428cc98fd93 to your computer and use it in GitHub Desktop.
WordPress: Use Jetpack's Contact form to protect download links by providing links in form's success message.
<?php
/**
* Use Jetpack's Contact form to provide download links (in success message).
*
* Filter the Jetpack Contact Form's message returned after a successful submission.
* Put download links in a custom field to display in success message.
*
* @param string $r_success_message Success message.
* @return string $r_success_message Success message.
*/
function my_download_form_links( $r_success_message ) {
$post_id = get_the_ID();
$dl_links = get_post_meta( $post_id, 'my_download_links', true );
$dl_head = get_post_meta( $post_id, 'my_download_heading', true );
$heading = ($dl_head) ? $dl_head : 'Your download links:';
if ( get_post_type( $post_id ) == 'orange' && ! empty( $dl_links ) ) {
// Add heading to success message.
$r_success_message = "<nav class="download-links"><h2>$heading</h2>";
// Display custom field content (with downbload links).
$r_success_message .= wpautop( $dl_links ) . '</nav>';
}
return $r_success_message;
}
add_filter( 'grunion_contact_form_success_message', 'my_download_form_links', 10, 1 );
/**
* Add a legend to the form.
* Filter the HTML of the Jetpack Contact Form.
*
* @param string $r Contact Form HTML output.
* @param string $field_label Field label.
* @param int|null $id Post ID.
* @return string $r Contact Form HTML output.
*/
function my_download_form( $r, $field_label, $post_id ) {
$dl_links = get_post_meta( $post_id, 'my_download_links', true );
$dl_legend = get_post_meta( $post_id, 'my_download_legend', true );
$legend = ($dl_legend) ? $dl_legend : 'Please complete form to download files';
if ( get_post_type( $post_id ) === 'my_post_type' && 'My First Field' == $field_label ) {
$r = "<legend><h2>$legend</h2></legend>" . $r;
}
return $r;
}
add_filter( 'grunion_contact_form_field_html', 'my_download_form', 10, 3 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment