Standalone PHP File mit Wordpress Funktionen für Downloads zu denen der User angemeldet sein muss

Manchmal ist es sinnvoll eine eigenständige PHP Datei zu haben, fast ohne Wordpress, die aber auf Wordpress Funktionen zurück greifen kann. Z.b. um zu kontrollieren ob ein User angemeldet ist. Das folgende Beispiel ist ein Code Snippet, welches einen Download-Pfad annonymisiert und kontrolliert ob ein Nutzer angemeldet ist.

In dem nachfolgendem Beispiel Script, handelt es sich um eine download.php File im Root Folder Wordpress. Wenn sie direkt aufgerufen wird, bricht das Script ab und leitet den Nutzer auf die Startseite zurück. Wenn der User jedoch eingeloggt ist, dann wird dynamisch eine File created, heruntergeladen und nach dem download gelöscht. Das kann nützlich sein, wenn man z.B. einen Export von sensitiven Daten aus dem Backend oder am Ende einer Bestellung anbieten möchte. Dann könnte man dem Script eine $ID übergeben, das Script zieht sich alle Daten, erstellt eine CSV Datei, bringt diese zum Download und nach dem Download wird sie direkt gelöscht. Nutzer von außen hätten keine Public URL um auf die CSV Datei zu zugreifen und selbst, wenn sie die PHP Datei und die $ID kennen würden, müssten sie sich ins Wordpress einloggen.

<?php 
/**-----------------------------------------------------------
 * Using Wordpress & WP-Theme Functions 
 * ----------------------------------------------------------- 
 * 
 * This script enabales to use methods, classes and everything
 * from wordpress and your theme in a 'standalone' file.
 * 
 * This is an example to force a download of a hidden file.
 * But it only works if the user is logged in into wp.
 * After the Download the file will be deleted.
 * 
 * ----------------------------------------------------------- 
 */
ignore_user_abort( true );
 
/* ========================================================= *\
	Require WP Functions
\* ========================================================= */
$wp_bootstrap_path = 'wp-load.php';
define( 'WP_USE_THEMES', false ); 
require_once $wp_bootstrap_path;
 
 
/* ========================================================= *\
	Check if User is Logged into WP
\* ========================================================= */
if( !is_user_logged_in() ){ // Redirect if user is logged out
	$redirect_to     = get_home_url();
	$redirect_status = 302;
	wp_redirect( $redirect_to, $redirect_status );
	exit;
}
 
 
 
/* ========================================================= *\
	Generate the File to Download
	- this could be a static file as well of course
\* ========================================================= */
$current_date       = date( 'Y-m-d' );
$download_file_path = "my_dynamic_dummy_file.{$current_date}.txt";
file_put_contents( $download_file_path, "Some Text in a new File for downloading." );
 
 
 
/* ========================================================= *\
	Download of File
\* ========================================================= */
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($download_file_path) . "\""); 
readfile( $download_file_path ); 
 
 
/* ========================================================= *\
	Delete File after Download is complete
\* ========================================================= */
unlink( $download_file_path );
 
if( connection_aborted() ) { // Also delete file if user disconnects/aborts download
    unlink( $download_file_path );
}

Für noch mehr Sicherheit, könnte ein System mit wp-nonce gebaut werden, um einen Request noch weiter abzusichern, bzw. dessen Gültigkeit zu kontrollieren.


Page Tools