Skip to content

Instantly share code, notes, and snippets.

@Ollo
Last active December 23, 2015 18:39
Show Gist options
  • Save Ollo/6677006 to your computer and use it in GitHub Desktop.
Save Ollo/6677006 to your computer and use it in GitHub Desktop.
Push a file download after registration. - hides url of file to prevent unauthorized sharing - uses custom field in ACF to store download file - uses gravity forms for form management
add_action("gform_after_submission_1", "push_pdf", 10, 2);
function push_pdf($entry, $form) {
$host = $_SERVER['HTTP_HOST'];
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
global $post;
$postID = $post->ID;
$filePath = get_field('file_download', $postID);
$file = $filePath;
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['HTTP_HOST'];
$urlFileToDl = str_replace($protocol.$domainName.'/',"", $file);
if(is_file($urlFileToDl)) {
$type = "application/pdf";
ob_start();
header('Content-Description: File Transfer');
header("Content-disposition: attachment; filename=".basename($urlFileToDl));
header("Content-Type: $type");
header("Content-Transfer-Encoding: $type\n" );
header("Content-Length: ".filesize($urlFileToDl));
ob_clean();
flush();
readfile( $urlFileToDl);
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment