0

I have code:

class some{

     public function run(){

            add_filter('query_vars', array( $this, 'addPar'));   
            $this->some_body();
     }


     public function addPar($vars){

      $vars[] = "my_par"; 
      return $vars;

    }

    public function some_body(){


     if(isset( get_query_var('my_par')) {

      $value = get_query_var('my_par'); 

      echo $value;

      }

    echo '<li><a href="'.esc_url(add_query_arg(array('my_par' => '4'))).'">></a></li>'; 


    }


}

in my plugin i call:

$action = new some();
$action->run()

Nothing should appear on the start, but after clicking on the link the variable should already be registered but I get null.

From `var_dump(get_query_var('my_par')):

object(WP_Query)[268]
  public 'query' => null
  public 'query_vars' => 
    array (size=0)
      empty
  public 'tax_query' => null
  public 'meta_query' => boolean false
  public 'date_query' => boolean false

...

my_par is not registered.

  • I change little code but idea is the same - after click link variable is not registered in WP Query in "public query_vars". – polyman Apr 3 at 11:41
  • Can you see something in debug.log? – huraji Apr 3 at 19:05
  • i have define WP_DEBUG, WP_DEBUG_LOG and SCRIPT_DEBUG as TRUE but i not have debug.log file in /wp-content. Plugin WP Live Debug display nothing. I testing my plugin in localhost. – polyman Apr 3 at 19:17
  • Ok, I'd suggest to fix this issue before, it could be really helpful. – huraji Apr 3 at 19:23
  • Debug display nothing. I testing my plugin in localhost. – polyman Apr 3 at 19:24
1

some_body needs to be run as an action on pre_get_posts.

Here's an example of the entire process of adding new query vars.

Pay extra attention to myplugin_pre_get_posts:

function myplugin_pre_get_posts( $query ) {
...
    $city = get_query_var( 'city' );
...
}
add_action( 'pre_get_posts', 'myplugin_pre_get_posts', 1 );
  • can you look in my new code? – polyman Apr 4 at 22:24
  • I just saw admin.php in your code. If this is something you're doing in the WordPress admin, this is not the right way to do it. Take a look at wordpress.stackexchange.com/questions/91693/… and the WordPress Codex for Admin Pages: codex.wordpress.org/Adding_Administration_Menus – MikeNGarrett Apr 5 at 13:50
  • I call "admin" in title question. I have menu and now in some in content in admin panel in my plugin i must get variables from $_GET, and of course i can do it $_GET['some'] but i want know how do it correctly. – polyman Apr 5 at 17:02
  • Yes, I understand. Adding query vars is not what you should be doing here. That's for matching patterns on the front-end to map url structure to particular query arguments. This is not intended use for any of this. – MikeNGarrett Apr 5 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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