forked from chrisguitarguy/WPSE-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-disposition-download.php
94 lines (79 loc) · 2.64 KB
/
content-disposition-download.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
Plugin Name: Download Example
Plugin URI: http://wordpress.stackexchange.com/questions/27232/get-wordpress-login-functions-without-printing-anything
Description: An example of how to do file downloads in WordPress with `Content-Disposition`
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action('init', 'wpse27232_add_rewrite');
/**
* Adds the rewrite rule for the download.
*
* @uses add_rewrite_rule
*/
function wpse27232_add_rewrite()
{
add_rewrite_rule(
'^download/?$',
'index.php?file_download=true',
'top'
);
}
add_filter('query_vars', 'wpse27232_query_vars');
/**
* Filter our query vars so WordPress recognizes 'file_download'
*/
function wpse27232_query_vars($vars)
{
$vars[] = 'file_download';
return $vars;
}
add_action('template_redirect', 'wpse27232_catch_file_dl');
/**
* Catches when the file_download query variable is present. Sends the content
* header, the file, and then exits.
*/
function wpse27232_catch_file_dl()
{
// No query var? bail.
if(!get_query_var('file_download')) return;
// change this, obviously. Should be a path to the pdf file
// I wrote this as a plugin, hence `plugin_dir_path`
$f = plugin_dir_path(__FILE__) . 'your-file.pdf';
if(file_exists($f) && is_user_logged_in())
{
// Do your additional checks and setup here
// Send the headers
header('Content-Type: aplication/pdf');
header('Content-Disposition: attachment; filename=' . basename($f));
header('Content-Length: ' . filesize($f));
// You may want to make sure the content buffer is clear here
// read the pdf output
readfile($f);
exit();
}
else
{
global $wp_query;
$wp_query->is_404 = true;
}
}
register_activation_hook(__FILE__, 'wpse27232_activation');
function wpse27232_activation()
{
wpse27232_add_rewrite();
flush_rewrite_rules();
}