Attach middleware to specific existing route

Viewed 1354

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. 
});
2 Answers
Related