Is there a way to attach middleware to a specific route in Wordpress or just in PHP? I'd like to run a middleware function before allowing access to the uploads folder to check if the user has access to the file before allowing them to download it.
I come from a background in node.js/express so if it helps I'd like to do something like this:
app.use('/wp-content/uploads', function(req, res, next) {
// do stuff with req and call next to continue,
// or use res to end the request early.
});
There are various WordPress plugins for restricting access to content or downloads, mostly based on the logic of been registered or not and what privileges that user has.
Basic logic behind them is like this :
// Redirect guests
function guest_redirect() {
$guest_routes = array(
'member-login',
'member-account',
'member-register',
'member-password-lost',
'member-password-reset'
);
// Force login or registration
if ( !is_user_logged_in() && !is_page($guest_routes) ) {
wp_redirect( 'member-login' );
exit;
}
}
add_action( 'template_redirect', 'guest_redirect' );
Also, WordPress has REST API (official doc here for your need). However, using own custom thing for production site using REST API without properly testing can be risky and complicated to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With