Last active
August 29, 2015 14:10
-
-
Save smeric/cb4520e848aaa0be703c to your computer and use it in GitHub Desktop.
WordPress plugin used to allow attachment download with one click.
This file contains 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
<?php | |
/** | |
* Force attachments download : | |
* | |
* In order for this to work, when you insert the file into your post through the media uploader, | |
* you have to select Post URL rather than File URL in the Link URL field before inserting into your post. | |
* A link to a filename will follow the browser's preferences, but by linking to the WP post link, | |
* you can control its behaviour.) | |
* | |
* @link https://gist.github.com/smeric/cb4520e848aaa0be703c | |
* @version 1.0.0 | |
* @package downloadable-attachments | |
* @author Sébastien Méric <[email protected]> | |
*/ | |
if ( have_posts() ) : | |
while ( have_posts() ) : | |
the_post(); | |
// Get attachment informations | |
$uploads_dir = wp_upload_dir(); | |
$attachment_src = get_post_meta( $post->ID, '_wp_attached_file', true ); | |
$file = path_join( $uploads_dir['basedir'], $attachment_src ); | |
$filename = basename( get_attached_file( $post->ID ) ); | |
$filetype_and_ext = wp_check_filetype_and_ext( $file, $filename ); | |
$filename = $post->post_title | |
? sanitize_title( $post->post_title ) . '.' . $filetype_and_ext['ext'] | |
: $filetype_and_ext['proper_filename'] | |
? $filetype_and_ext['proper_filename'] | |
: $filename; | |
//$mime_type = get_post_mime_type( $post->ID ); | |
$mime_type = $filetype_and_ext['type'] | |
? $filetype_and_ext['type'] | |
: 'application/octet-stream'; | |
// Return file to download | |
error_reporting( 0 ); // Errors may corrupt download | |
ob_start(); | |
header( "Content-Description: File Download" ); | |
header( "Pragma: public" ); // Required | |
header( "Expires: 0" ); | |
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); | |
header( "Cache-Control: private", false ); // Required for certain browsers | |
//header( "Content-Type: application/octet-stream" ); | |
header( "Content-Type: $mime_type" ); | |
header( "Content-Disposition: attachment; filename=\"$filename\";" ); | |
header( "Content-Transfer-Encoding: binary" ); | |
//header( "Content-Length: " . filesize( $file ) ); | |
ob_clean(); | |
ob_end_flush(); | |
readfile( $file ); | |
exit; | |
endwhile; | |
endif; | |
?> |
This file contains 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
<?php | |
/** | |
* Downloadable attachments | |
* | |
* Use this plugin if you want that the attachment page link click triggers media download. | |
* | |
* @link https://gist.github.com/smeric/cb4520e848aaa0be703c | |
* @version 1.0.0 | |
* @package downloadable-attachments | |
* @author Sébastien Méric <[email protected]> | |
* | |
* @wordpress-plugin | |
* Plugin Name: Downloadable attachments | |
* Plugin URI: https://gist.github.com/smeric/cb4520e848aaa0be703c | |
* Description: Use this plugin if you want that the attachment page link click triggers media download. | |
* Version: 1.0.0 | |
* Author: Sébastien Méric | |
* Author URI: http://www.sebastien-meric.com/ | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: downloadable_attachments | |
* Domain Path: /languages | |
* | |
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU | |
* General Public License as published by the Free Software Foundation; either version 2 of the License, | |
* or (at your option) any later version. | |
* | |
* 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. | |
* | |
* 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 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
/** | |
* downloadable_attachments_template_include | |
* | |
* Inckude custom attachment template instead of theme one. | |
* | |
* @since 1.0.0 | |
* @package downloadable-attachments | |
* @author Sébastien Méric <[email protected]> | |
**/ | |
add_filter( 'template_include', 'downloadable_attachments_template_include' ); | |
function downloadable_attachments_template_include( $original_template ) { | |
$new_template = $original_template; | |
if ( is_attachment() ) { | |
$new_template = plugin_dir_path( __FILE__ ) . 'attachment.php'; | |
} | |
return $new_template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment