duanjianao0592 2013-01-24 12:55
浏览 127

Wordpress - 如何定义城市下拉值

I am working on an existing wordpress website. Users has field "user-country" (actually, I do not know how this field is created in wordpress, but it works). In the registration form, user can choose one specific country. However, now this country list is note defined "anywhere". It is created explicitly in the code:

<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


The client wants to changed this list (from country to city). So i need to add other values. I do not want to write all values in the code. I would like to create some list with these values in wp-admin.

What is the best way to create a predefined values list? And these are not custom fields for posts.

EDIT: I want to store values in DB, so admin can modidfy these values from wp-admin. Actually, it is not so important whether it is DB or other option like XML. I just want this list to appear as dropdown when user is registering and also to wp-admin to modify values of this list.

Also, a question come to my mind - is it a normal practice to store user custom fields like country or city in DB? Or maybe it is ok to define them in code explicitly?

  • 写回答

1条回答 默认 最新

  • ds000001 2013-01-24 14:44

    Well, if you want the administrator to be able to modify the list, then DB is likely the best option here.

    I would do something like this (in WordPress):

    // put a default (initial) list in the database, if there isn't one there yet
      // 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);

    Now, later where you need that list, simply get it from the db:

    $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);

    I guess you'll also have some kind of administration form for the options, where the administrator can modify entries from this list. This could be a textarea. When it gets submitted you update_option() again (replace new lines with |)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中