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

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?