0

I've written a quick little plugin to grab some custom query_vars, do some manipulation, and store the output in a CSV file. It works great for pages and posts, but ... it seems that queries to wp-content/uploads don't actually go through WordPress, and I want to grab those, too.

I'm thinking of two different approaches:

  1. per the answer to this question, maybe write a custom rewrite rule, and pass the request into WordPress iff it's a query for wp-content/uploads and there exists a query string; or,

  2. write a standalone PHP program that processes the request from a custom rewrite rule, without worrying about spinning up WordPress.

The problem with (1) is that ... the Internet is a wild and whacky place. While I imagine there will be far fewer requests for wp-content/upload content without query strings than with, it still could end up being a ton of extra load on the server, depending on what happens out there in the wild.

The problem with (2) is, I have to either figure out how to figure out wp_upload_dir and plugin_dir_path without spinning up wordpress, or I have to stick everything outside of the wordpress hierarchy.

At the moment, I can do the latter, as this is just a quick little hack for one of my sites, but ... it feels so wrong. Is there a better way to do this?

2
  • sorry, I do not full understand the question, you want to know how to get all the file names in .../wp-content/uploads/? you want to know how to get the real path to .../wp-content/uploads/? or is something else?! Commented Nov 11, 2021 at 6:12
  • If someone hits ../wp-content/uploads/2012/10/img.png?attr1=foo&attr2=bar I want to get attr1 and attr2. Commented Nov 11, 2021 at 10:25

1 Answer 1

1

The problem, as you noted is with the .htaccess rewrite rules which - in the typical wp htaccess file - exclude physical files and directories from being processed by index.php and got served directly. I think the solution is in working with .htaccess adding a prioritary rule to let a file with query string being processed by php instead of being served directly and loose the query string. I don't think such a rule (well restricted to specific cases) may overload the server, you can easily monitor it and decide wether or not to use it: try this in your .htaccess:

#first condition if the URL contains the path to uploads directories
RewriteCond %{REQUEST_URI}  /wp-content/uploads/ [NC] 

#second condition if specific keys are present in the query string (attr1 OR attr2)
RewriteCond %{QUERY_STRING} attr1= [OR]
RewriteCond %{QUERY_STRING} attr2=

#third condition we need to avoid infinite loop and errror 500, 
#so we check that a specific key (added in our final rewrite rule below) is not present 
RewriteCond %{QUERY_STRING} !loop=no

#the final rule if all above is matched will redirect to index.php and add our 'loop=no' key/value pair to avoid the loop
RewriteRule ^ /index.php?loop=no [L,QSA] 

At this point if you type your url /wp-content/uploads/2012/10/img.png?attr1=foo&attr2=bar you will get 404 not found. In your plugin function then:

add_action('init','check_attributes');
function check_attributes(){
  if(!empty($_GET['loop']) && $_GET['loop'] =='no'){ //you may add more clause here if needed

    $var1=$_GET['attr1'];
    $var2=$_GET['attr2'];
    // perform your logic with the variables passed to the url
   
    // maybe redirect to the physical file without query vars?
    wp_redirect(strtok($_SERVER["REQUEST_URI"], '?'));
    exit;
  }
}

Hope it may help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.