H
H
hitakiri2016-01-18 18:01:25
PHP
hitakiri, 2016-01-18 18:01:25

How to intercept get request in wordpress?

When the page is rendered, several get requests are made to load images.

GET: mysite.loc/cat/page1.html
GET: mysite.loc/cat/images/img1.jpg
GET: mysite.loc/cat/images/img2.jpg
...

Previously, the presence of requests for an image was determined simply - through apache mod_rewrite. When determining a request for an image, a script was launched that replaced the titles with the necessary ones. Those. in .htaccess there was a rule:
RewriteRule ^([a-zA-z0-9]+)/(.*\.(jpg|jpeg|png|gif))$ /mysript.php?imgpath=$1/$2 [NC,L]

Rewriting the script as a plugin for WP, I ran into the problem that the engine does not store anything in the global $_GET. In fact, the exact imgpath value itself does not interest me, but it is necessary to know about its existence (or absence) in order to substitute the headers at the right time. I tried to use the add_rewrite_tag()
function built into the wp api , but it didn't help. fulfills at the level of the first request.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pingo, 2016-01-18
@pingo

can stick

@file_put_contents('log_get.tmp', $_GET['image'],  FILE_APPEND);
and already look in the file log_get.tmp

D
Dmitry, 2016-01-20
@dimasmagadan

I think you are uploading wrong.
but, as an option, you can implement your task like this,
add endpint
add_rewrite_endpoint( 'imgpath', EP_ALL );
https://codex.wordpress.org/Rewrite_API/add_rewrit...
in functions.php add this

function prefix_output() {
  switch (get_query_var( 'imgpath' )) {
// тут делаем что нужно с пойманной переменной
    default:
      break;
  }
  remove_action( 'template_redirect', 'prefix_output' );
 	exit; 
}

function prefix_custom_get_vars(){
  global $wp_query;

    if( !in_array($wp_query->get( 'imgpath' ), array('разрешенные значения' ) ) && !is_admin() ) {
        return;
    }
  add_action( 'template_redirect', 'prefix_output' );
}
add_action( 'pre_get_posts', 'prefix_custom_get_vars' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question