English 中文(简体)
Wordpress 选项页面和回回回功能循环
原标题:Wordpress options page and looping through callback functions

I m 创建一个简单的 WordPress 插件, 该插件将在 设置 菜单中设置一个选项页面, 供客户端添加业务细节 。

我登记我所有田地的地址是:

// Lets set an array for the inputs:
$fields = array (
  array( "name",           "Business Name:"),
  array( "tagline",        "Business Tagline:"),
  array( "logo",           "Business Logo:"),
  array( "owner_name",     "Owner s Name:"),
  array( "owner_title",    "Owner s Title"),
  array( "address",        "Address:"),
  array( "city",           "City:"),
  array( "province",       "Province:"),
  array( "country",        "Country:"),
  array( "phone",          "Phone:"),
  array( "secondary_phone","Secondary Phone:"),
  array( "fax",            "Fax:"),
  array( "toll_free",      "Toll Free:"),
  array( "email",          "Email:"),
  array( "website",        "Website:"),
);

foreach($fields as $field) {
  //id, title (label), callback, page, section(from add_settings_section), args
  add_settings_field("business_{$field[0]}", $field[1], "business_{$field[0]}_setting", __FILE__,  main_section );
}

此简单的环绕在数组中的设置, 添加所有我需要的字段, 并用 < code> business@$field > 设置回调函数的引用 。 [0] __ 设置 。

然后,我必须给每一个人创建回调功能,比如:

function business_name_setting() {
  $options = get_option( plugin_options );
  echo "<input name= plugin_options[business_name]  type= text  value= {$options[ business_name ]}  />";
}

我假设有更优雅的方式这样做, 因为单凭个人创造所有回声 将是极其多余的, 当它们基本上会是相同的时。

问题回答

解决办法如下:

add_settings_field 函数需要第6个参数, 并将其传递到回调函数。 我已经发送了 $field[0] 的值。 我还设置了 add_settings_field 函数, 将所有回调发送到现在称为 business_section 的处理器函数 。

新的前方圆环:

foreach($fields as $field) {
       //id, title (label), callback, page, section(from add_settings_section), args
       add_settings_field("business_{$field[0]}", $field[1], "business_setting", __FILE__,  main_section , $field[0]);
   }

新的回调功能现已通过我们先前的 $field[0] 值, 现在可以使用该密钥创建正确的输入 :

function business_setting($field) {
  $options = get_option( plugin_options ); 
  $full_field =  business_ .$field;
  echo "<input name= plugin_options[{$full_field}]  type= text  value= " . $options[$full_field] . "  />";
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签