Skip to content

Instantly share code, notes, and snippets.

@sc0ttkclark
Last active October 24, 2021 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sc0ttkclark/39983763facf5f667c859ae2c3b083bc to your computer and use it in GitHub Desktop.
Save sc0ttkclark/39983763facf5f667c859ae2c3b083bc to your computer and use it in GitHub Desktop.
Handle .php files using the getfile in PMPro integration.
# Add this to your .htaccess code just like the normal getfile.php code.
# Follow instructions here first and then add the line: https://www.paidmembershipspro.com/locking-down-protecting-files-with-pmpro/
RewriteRule ^protected-directory/(.*)$ /wp-content/plugins/paid-memberships-pro/services/getfile.php [L]
<?php
/**
* Handle .php files using the getfile in PMPro integration.
*
* You will need to use another extra htaccess rule along with the normal getfile.php handling as documented at:
* https://www.paidmembershipspro.com/locking-down-protecting-files-with-pmpro/
*
* The custom htaccess rule would be something like:
*
* RewriteRule ^protected-directory/(.*)$ /wp-content/plugins/paid-memberships-pro/services/getfile.php [L]
*
* This would work similar to -- but would not download the file it would read it -- for the non-WP files solution
* documented at:
* https://www.paidmembershipspro.com/locking-non-wordpress-files-folders-paid-memberships-pro/
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
/**
* Check if the file is a .php file and then include it and stop the normal getfile.php process.
*
* @param string $filename The filename being downloaded.
*/
function my_pmpro_getfile_for_php_files( $filename ) {
// Only run for .php files.
if ( '.php' !== substr( $filename, -4, 4 ) ) {
return;
}
// Only run if the file is in the /protected-folder/ we want.
if ( 0 !== strpos( $filename, ABSPATH . 'protected-folder/' ) ) {
return;
}
// Check if they have the access required.
if ( ! pmpro_hasMembershipLevel( [ 1, 2, 4 ] ) ) {
// Hook for users without access.
do_action( 'pmpro_getfile_before_error', $filename, 0 );
header( 'HTTP/1.1 503 Service Unavailable', true, 503 );
echo 'HTTP/1.1 503 Service Unavailable';
exit;
}
include $filename;
exit;
}
add_action( 'pmpro_getfile_before_readfile', 'my_pmpro_getfile_for_php_files' );
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( 'You do not have access to this file.' );
}
// Do whatever you'd like to do in your PHP file here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment