最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Get url param no longer works when using add_rewrite_rule

programmeradmin2浏览0评论

I'm using WordPress admin to create a page (named get-region-page), in which Pod shortcode is used to retrieve the URL parameter. For example:

[pods name="region" slug="{@get.regionparam}" field="region_name"]

With a redirection 301 to the URL /get-region-page&regionparam=$region_slug, the Pod shortcode works fine.

The problem arrives when I need to create a rewrite rule for that page in functions.php (with 1234 is the page ID for get-region-page):

add_rewrite_rule('({some-custom-regex})/?$','index.php?page_id=1234&regionparam=$matches[1]','top');

...the parameter becomes null. I can no longer retrieve the URL parameter. But if I try this PHP code in the WordPress template of the page, the parameter is still there:

echo get_query_var('regionparam');

Remark, this one does not work either in the PHP template:

echo $_GET['regionparam'];

So I wonder if there is some equivalent Pod shortcode for get_query_var('regionparam') for my first Pod shortcode to work again?

I'm using WordPress admin to create a page (named get-region-page), in which Pod shortcode is used to retrieve the URL parameter. For example:

[pods name="region" slug="{@get.regionparam}" field="region_name"]

With a redirection 301 to the URL /get-region-page&regionparam=$region_slug, the Pod shortcode works fine.

The problem arrives when I need to create a rewrite rule for that page in functions.php (with 1234 is the page ID for get-region-page):

add_rewrite_rule('({some-custom-regex})/?$','index.php?page_id=1234&regionparam=$matches[1]','top');

...the parameter becomes null. I can no longer retrieve the URL parameter. But if I try this PHP code in the WordPress template of the page, the parameter is still there:

echo get_query_var('regionparam');

Remark, this one does not work either in the PHP template:

echo $_GET['regionparam'];

So I wonder if there is some equivalent Pod shortcode for get_query_var('regionparam') for my first Pod shortcode to work again?

Share Improve this question edited Apr 3, 2020 at 11:22 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Apr 3, 2020 at 10:28 bdkbdk 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I don't know why my question was not well formatted and can not modify it. Here's the more readable one:

I'm using WP Admin DIVI builder to create a page (named get-region-page), in which Pod shortcut is used to retrieve the url param. For example:

[pods name="region" slug="{@get.regionparam}" field="region_name"]

which gives apparently the same result as using these in the php template of the page:

$_GET['regionparam']

With a redirection 301 to that page url: /get-region-page&regionparam=$region_slug, everything works fine, I can retrieve the url parameter. The problem arrives when I replace the 301 redirection with a rewrite rule for that page in functions.php (with 1234 the page id for get-region-page):

add_rewrite_rule('({some-custom-regex})/?$','index.php?page_id=1234&regionparam=$matches[1]','top');

...the parameter regionparam becomes null. Only this works:

get_query_var('regionparam');

but no longer this

$_GET['regionparam'];

So I wonder if there is any way I can retrieve that get_query_var('regionparam') for my first pod shortcut to rework again in DIVI builder? Thank you for your help!

I found the solution, rather a workaround for my problem. The url parameter can still be retrieved by using $GLOBALS[ ‘any’ ] thanks to this document: https://docs.pods.io/displaying-pods/magic-tags/special-magic-tags/. So instead of using $_GET['any']as in my previous shortcode:

[pods name="region" slug="{@get.regionparam}" field="region_name"]

I just replace it with $GLOBALS:

[pods name="region" slug="{@globals.regionparam}" field="region_name"]

In order for the parameter regionparam to be available as global, I needed to add this filter in functions.php:

add_filter("query_vars", function( $vars ){
    $vars[] = "regionparam";
    $vars[] .= "otherParamsIfNeeded";
    return $vars;
});

And in the other functions, I replace the old $_GET['regionparam'] by get_query_var('regionparam').

发布评论

评论列表(0)

  1. 暂无评论