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

etc.

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

    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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。