admin管理员组文章数量:1278918
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:
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,
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?
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:
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,
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?
Share Improve this question asked Nov 10, 2021 at 14:21 philolegeinphilolegein 2832 silver badges12 bronze badges 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?! – tiago calado 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. – philolegein Commented Nov 11, 2021 at 10:25
1 Answer
Reset to default 1The 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
本文标签: plugin developmentHow to grab query string from wpcontentuploads*
版权声明:本文标题:plugin development - How to grab query string from wp-contentuploads.* 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741225073a2361705.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论