简体   繁体   English

Wordpress add_filter('query_vars', 'my_new_vars'); 没有按预期工作

[英]Wordpress add_filter('query_vars', 'my_new_vars'); not working as expected

I have an issue with Wordpress (there's a shocker), where it removes my get parameter, which i understand thats a WP feature for security and some other reasons.我对 Wordpress 有一个问题(有一个令人震惊的地方),它删除了我的 get 参数,我知道这是出于安全性和其他一些原因的 WP 功能。

What i'm trying to achieve is the following:我想要实现的是以下几点:

  1. Load product page加载产品页面
  2. When customer clicks book now they are redirected to an enquire now form当客户点击立即预订时,他们将被重定向到立即查询表单
  3. On enquire now form there is widget that retrieves what product the customer was looking at and using a GET parameter i can retrieve this product在现在查询表单中有一个小部件可以检索客户正在查看的产品并使用 GET 参数我可以检索该产品

I've tried to add the get parameter as follows:我尝试按如下方式添加 get 参数:

# functions.php

function gpd_register_query_vars($vars)
{
    $vars[0] = 'my_product_id';
    return $vars;
}

add_filter('query_vars', 'gpd_register_query_vars');

Within my widget在我的小部件中

class GPD_Get_Product_Widget extends WP_Widget
{
    // ...

    function widget($args, $instance)
    {
        global $wp_query;
        var_dump($wp_query->query_vars['my_super_unique_var']);

        extract($instance);
        //output code
        echo $args['before_widget'];
        include 'widget.php';
        echo $args['after_widget'];
    }
}

//function to register the widget
function gpd_get_product_widget()
{
    register_widget('GPD_Get_Product_Widget');
}

add_action('widgets_init', 'gpd_get_product_widget');

However, whenever i try to get the parameter it doesn't exist.但是,每当我尝试获取参数时,它都不存在。

Wordpress isn't the easiest to navigate or work with. Wordpress 不是最容易导航或使用的。 I'm really confused to why WP has made such a simple thing such as $_GET params so difficult.我真的很困惑为什么 WP 让$_GET参数这样简单的事情变得如此困难。

Any help is much appreciated.任何帮助深表感谢。

I found the answer and not entirely sure why this is but if you pass 2 params in your URL like so /my-page?a=1&b=2 and then use a plain old $_GET , you'll find that the the first element is a q and the second is your b param.我找到了答案,但不完全确定这是为什么,但是如果您像这样/my-page?a=1&b=2在 URL 中传递 2 个参数,然后使用普通的旧$_GET ,您会发现第一个元素是一个q ,第二个是你的b参数。

array(2) {
  'q' =>
  string(29) "/request-a-quote/a=1"
  'b' =>
  integer(1) "2"
}

It looks like the first param is occupied by q (reserved var) by Wordpress and anything after that is additional params which are yours (unless they are reserved by WP).看起来第一个参数被 Wordpress 的q (保留的 var)占用,之后的任何内容都是您的附加参数(除非它们被 WP 保留)。

So if I was to build my URL like so:因此,如果我要像这样构建我的 URL:

add_query_arg(['type' => 'holiday', 'product_id' => 12345], get_permalink($page_id) );

You have to add a first param that will be ignored and then the second will be available as a $_GET .您必须添加将被忽略的第一个参数,然后第二个参数将作为$_GET可用。

I might be doing something wrong but this works for me for now.我可能做错了什么,但这对我现在有用。 Any help and pointers to what I'm doing wrong would be great - as this feels wrong but works.任何关于我做错了什么的帮助和指示都会很棒 - 因为这感觉不对但有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM