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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵