Last active
October 1, 2018 08:00
-
-
Save KZeni/9e6d806c1aee81985ca0b3a0e935e125 to your computer and use it in GitHub Desktop.
Make Download Monitor's native download permalinks (which some search services grab instead of the custom endpoint in the plugin's settings) resolve to the correct download on the site. Add the `content-download-url-only.php` file to your WordPress theme and add the `functions snippet.txt` code snippet to your theme's `functions.php` file.
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
<?php | |
/** | |
* Simply outputs the URL for the download | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} // Exit if accessed directly | |
?> | |
<?php $dlm_download->the_download_link(); ?> |
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
// Convert raw ?post_type=dlm_download&p=* URLs into pointing to the correct location (relies on the custom url-only download template to retrieve the correct URL) | |
function dlm_download_raw_url_redirect($query) { | |
if(isset($_GET['post_type']) && $_GET['post_type'] == 'dlm_download' && isset($_GET['p']) && !is_admin() && !wp_doing_ajax()){ | |
$download_id = $_GET['p']; | |
$failed_to_retrieve_download = false; | |
// Action on found download | |
if ( $download_id > 0 ) { | |
$download_url = do_shortcode('[download id="'.$download_id.'" template="url-only"]'); | |
if(strpos($download_url, 'http') !== false){ // Returned value is a URL | |
wp_redirect($download_url); | |
exit(); | |
}else{ | |
$failed_to_retrieve_download = true; | |
} | |
}else{ | |
$failed_to_retrieve_download = true; | |
} | |
if($failed_to_retrieve_download){ | |
if ( $redirect = apply_filters( 'dlm_404_redirect', false ) ) { | |
wp_redirect( $redirect ); | |
exit(); | |
} else { | |
wp_die( __( 'Download does not exist.', 'download-monitor' ) . ' <a href="' . home_url() . '">' . __( 'Go to homepage →', 'download-monitor' ) . '</a>', __( 'Download Error', 'download-monitor' ), array( 'response' => 404 ) ); | |
} | |
} | |
} | |
} | |
add_filter('template_redirect', 'dlm_download_raw_url_redirect'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was further detailed at: WPChill/download-monitor#459 (comment)