gpt4 book ai didi

php - Wordpress - 如何定义城市下拉值

转载 作者:行者123 更新时间:2023-12-04 05:14:08 25 4
gpt4 key购买 nike

我正在一个现有的 wordpress 网站上工作。
用户有字段“用户国家”(实际上,我不知道这个字段是如何在 wordpress 中创建的,但它有效)。
在注册表中,用户可以选择一个特定的国家。
但是,现在该国家/地区列表被注释定义为“任何地方”。它是在代码中显式创建的:

<option value="Afghanistan" <?php if($current_user->user_country == 'Afghanistan') echo 'selected';?>>Afghanistan</option>
<option value="Albania" <?php if($current_user->user_country == 'Albania') echo 'selected';?>>Albania</option>
<option value="Algeria" <?php if($current_user->user_country == 'Algeria') echo 'selected';?>>Algeria</option>
<option value="American Samoa" <?php if($current_user->user_country == 'American Samoa') echo 'selected';?>>American Samoa</opt

等。

客户想要更改此列表(从国家到城市)。所以我需要添加其他值。我不想在代码中写入所有值。我想在 wp-admin 中使用这些值创建一些列表。

创建预定义值列表的最佳方法是什么?这些不是帖子的自定义字段。

编辑:
我想将值存储在数据库中,因此管理员可以从 wp-admin 修改这些值。
实际上,它是 DB 还是 XML 之类的其他选项并不那么重要。
我只希望此列表在用户注册时显示为下拉列表,并且还希望 wp-admin 修改此列表的值。

另外,我想到了一个问题 - 在 DB 中存储用户自定义字段(如国家或城市)是否正常?或者也许可以在代码中明确定义它们?

最佳答案

好吧,如果您希望管理员能够修改列表,那么 DB 可能是这里的最佳选择。

我会做这样的事情(在 WordPress 中):

// put a default (initial) list in the database, if there isn't one there yet
if(!get_option('my_country_list')){

// store it as a |-delimited string, because WP serializes arrays,
// and this would be too much here
$data = 'Albania|Algeria|Disneyland|etc';

update_option('my_country_list', $data);
}

现在,稍后您需要该列表时,只需从数据库中获取它:
$countries = get_option('my_country_list');

// turn it into an array
$countries = implode('|', $countries);

// generate the select field
$html = '';
foreach($countries as $country){

$checked = '';

if($current_user->user_country == $country)
$checked = 'selected="selected"';

$html .= sprintf('<option value="%1$s" %2$s> %1$s </option>', $country, $checked);
}

printf('<select> %s </select>', $html);

我想您还会有某种选项的管理表单,管理员可以在其中修改此列表中的条目。这可能是一个文本区域。当它被提交时,你 update_option()再次(用 | 替换新行)

关于php - Wordpress - 如何定义城市下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14501886/

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