gpt4 book ai didi

javascript - 如何将变量传递给functions.php(Wordpress 站点)中的函数

转载 作者:行者123 更新时间:2023-11-28 01:18:25 24 4
gpt4 key购买 nike

我有一个网站,可以检测用户的位置,并仅显示具有匹配城市分类的帖子。如果没有匹配,用户将被重定向到一个页面以选择可用城市。这是我的功能:

function my_location($q){
if (!$q->is_main_query())
return;
if ($q->is_search())
return;
if ($q->is_archive()){
if ( ! is_admin()) {
if ($userSlug!='Set'){
$userInfo = geoip_detect_get_info_from_current_ip();
switch ($userInfo->postal_code){
case '86403':
case '86404':
case '86405':
case '86406':
$city="lake-havasu-city";
break;
case '86401':
case '86402':
case '86409':
$city="kingman";
break;
case '86429':
case '86430':
case '86439':
case '86442':
$city="bullhead-city";
break;
default:
force_post_city($city);
exit;
}
$q->set( 'tax_query', array(array('taxonomy' => 'pa_city','field' => 'slug',terms' => array( $city ),'operator' => 'IN')));
}}
}
}
add_action( 'pre_get_posts', 'my_location' );

我的问题是,在用户选择城市的页面上,如何将城市传递回此函数,以便他们拉出适当的城市?这是我的表格:

    <form method="post" action="new_location($term_taxonomy)">
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a City</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->pa_city; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
$taxonomies = array('pa_city'); // CHANGE ME
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);
?>
<input type="submit" value="click" name="submit">

</form>

任何帮助将不胜感激!

最佳答案

session 建议是关键。我选择了 cookies 。因此,首先我添加了一个函数来检查 wordpress 的 init 上的 cookie。如果不存在,我们会尝试确定它们的位置并写入 cookie。然后在 pre_get_post Hook 中,我们将他们定向到包含城市优惠的页面,或者如果他们的城市与现有城市不匹配,我们将他们定向到搜索城市页面:

    add_action( 'init', 'my_setcookie' );
function my_setcookie() {
if(!isset($_COOKIE['city'])) {
$userInfo = geoip_detect_get_info_from_current_ip();
switch ($userInfo->postal_code){
case '86403':
case '86404':
case '86405':
case '86406':
$city="lake-havasu-city";
break;
case '86401':
case '86402':
case '86409':
$city="kingman";
break;
case '86429':
case '86430':
case '86439':
case '86442':
$city="bullhead-city";
break;
}
setcookie( 'city', $city, time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
}}
function my_location( $q ){
if (!$q->is_main_query() )
return;
if ($q->is_search())
return;
if ($q->is_archive() ){
if ( ! is_admin() ) {
if ($userSlug!='Set'){
if (empty($_COOKIE['city'] )) {
echo "<script>window.location.href = 'http://thewebsite.com/select-city/';</script>";
exit();
}
$city = isset( $_COOKIE['city'] ) ? $_COOKIE['city'] : 'not set';
$q->set( 'tax_query', array(array( 'taxonomy' => 'pa_city', 'field' => 'slug', 'terms' => array( $city ), 'operator' => 'IN' )));
}}
}
}

add_action( 'pre_get_posts', 'my_location' );

现在我们在搜索城市页面中找到了它们。我们从属性字段中提取可用城市并列出它们,单击后,将执行 header 中的 javascript 以更新 cookie 并将它们发送到交易:

            <form name="myCity" action="http://thewebsite.com/"  method="POST">
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a City</option>'";

foreach($myterms as $term){
$term_taxonomy=$term->pa_city; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}

$taxonomies = array('pa_city');
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);

?>
<input type="submit" value="click" name="submit" onclick="WriteCookie()">
</form>

这是 JavaScript,我们删除现有的 cookie 并设置新的:

                <script type="text/javascript">

function WriteCookie()
{
document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
cookievalue = escape(document.myCity.optionname.value) + ";"
document.cookie='city='+cookievalue +'; expires=Fri, 3 Aug 2021 20:47:11 UTC; path=/';
window.location.href = "http://thewebsite.com"

}

就像魅力一样!感谢 Halfer 的 session 建议。

关于javascript - 如何将变量传递给functions.php(Wordpress 站点)中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23550698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com