简体   繁体   English

自定义页面上的Wordpress更改URL参数

[英]Wordpress change URL parameter on custom page

I have a Wordpress site with a plugin in the backend which sets a $_GET parameter to my pages URL. 我有一个Wordpress网站,该网站的后端有一个插件,该插件将$_GET参数设置为我的页面URL。 Now I need to replace the value from this parameter with my custom value but this won't work: 现在,我需要使用自定义值替换此参数中的值,但这将无法正常工作:

My URL: http://localhost/wordpress/mein-konto/testpage/585/?conversationId=0 我的网址: http:// localhost / wordpress / mein-konto / testpage / 585 /?conversationId = 0

On the testpage/585 I've included a PHP file which get loaded on page load. 在testpage / 585上,我包含了一个PHP文件,该文件会在页面加载时加载。 In this file I have this function: 在此文件中,我具有以下功能:

add_query_arg( 'conversationId', 1234, get_permalink() );

Now I'm expecting this URL here: 现在,我希望在这里找到该URL:

http://localhost/wordpress/mein-konto/testpage/585/?conversationId=1234 HTTP://本地主机/ WordPress的/炒面-KONTO / testpage / 585 /的conversationId = 1234?

I'm not sure what I'm doing wrong. 我不确定自己在做什么错。

Try this: 尝试这个:

$conversationId = get_query_var( 'conversationId', '0' )

if ($conversationId === '0')
    wp_safe_redirect( add_query_arg( 'conversationId', 1234, get_permalink() ) )

It sounds like you are expecting add_query_arg to do the redirection, but it does not -- instead, it just returns a string that you can redirect to. 听起来好像您期望add_query_arg进行重定向,但事实并非如此,相反,它只是返回一个可以重定向到的字符串。 So, just put the returned string into a call to wp_safe_redirect and you are good! 因此,只需将返回的字符串放入对wp_safe_redirect的调用中,您就很好了!

However, before redirecting to the same page you need to check whether the redirection is necessary -- otherwise you'll end up in an endless redirection loop (that your browser will detect and stop). 但是,在重定向到同一页面之前,您需要检查是否有必要进行重定向-否则,您将陷入无休止的重定向循环(浏览器将检测并停止)。

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

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