Php 将自定义字段添加到存储地址部分的常规设置中
我想在Woocommerce设置页面中添加一个电话号码字段。我可以将该字段添加到设置列表中,但它会附加到整个选项卡的底部。理想情况下,我希望在“常规”选项卡上的“存储地址”部分添加该字段 以下是我现在使用的内容(精简): 我试图使用Php 将自定义字段添加到存储地址部分的常规设置中,php,wordpress,woocommerce,settings,custom-fields,Php,Wordpress,Woocommerce,Settings,Custom Fields,我想在Woocommerce设置页面中添加一个电话号码字段。我可以将该字段添加到设置列表中,但它会附加到整个选项卡的底部。理想情况下,我希望在“常规”选项卡上的“存储地址”部分添加该字段 以下是我现在使用的内容(精简): 我试图使用阵列拼接,但没有成功 如果您有任何建议,我们将不胜感激。此代码将在“门店地址”部分的“邮政编码”元素后添加您的电话号码。我从中借用了拼接函数 add_filter('woocommerce_general_settings'、'woocommerce_general_
阵列拼接
,但没有成功
如果您有任何建议,我们将不胜感激。此代码将在“门店地址”部分的“邮政编码”元素后添加您的电话号码。我从中借用了拼接函数
add_filter('woocommerce_general_settings'、'woocommerce_general_settings_add_phone_number');
功能商业\常规\设置\添加\电话\号码($settings){
$phone\u number=阵列(
“title”=>(电话号码),“woocommerce”),
“desc'=>”(添加您的电话号码),“woocommerce”),
'id'=>'woocommerce\u store\u phone\u number',
'默认'=>'',
'类型'=>'文本',
“描述提示”=>错误,
);
$array_pos=0;
foreach($设置为$key=>$value){
如果($value['id']='woocommerce\u store\u postcode'){
$array_pos=$key;
打破
}
}
$settings=array\u insert($settings,$phone\u number,$array\u pos+1);
返回$settings;
}
/*
数组插入
@数组要向其中添加元素的数组
@元素要添加到数组中的元素
@在数组中定位要添加元素的位置
*/
如果(!function_存在('array_insert')){
函数数组\插入($array,$element,$position){
//如果数组为空,只需将元素添加到其中
if(空($array)){
$array[]=$element;
//如果位置为负数
}elseif(是数值($position)&&$position<0){
//如果计数后为负位置
if(计数($array)+$position<0){
$position=0;
}否则{
$position=计数($array)+$position;
}
//以积极的姿态再试一次
$array=array\u insert($array,$element,$position);
//如果阵列位置已设置
}elseif(isset($array[$position])){
//将阵列拆分为两部分
$split1=array\u slice($array,0,$position,true);
$split2=array\u slice($array,$position,null,true);
//在两部分之间的位置添加新数组元素
$array=array\u merge($split1,array($position=>$element),$split2);
//如果未设置位置,则添加到数组末尾
}elseif(为空($position)){
$array[]=$element;
//如果未设置位置
}elseif(!isset($array[$position])){
$array[$position]=$element;
}
//清理索引
$array=数组值($array);
返回$array;
}
}
这可以通过一种简单的方法来完成,目标是woocommerce\u store\u postcode
字段ID,将store phone自定义字段插入此常规设置中的正确位置:
add_filter('woocommerce_general_settings', 'general_settings_shop_phone');
function general_settings_shop_phone($settings) {
$key = 0;
foreach( $settings as $values ){
$new_settings[$key] = $values;
$key++;
// Inserting array just after the post code in "Store Address" section
if($values['id'] == 'woocommerce_store_postcode'){
$new_settings[$key] = array(
'title' => __('Phone Number'),
'desc' => __('Optional phone number of your business office'),
'id' => 'woocommerce_store_phone', // <= The field ID (important)
'default' => '',
'type' => 'text',
'desc_tip' => true, // or false
);
$key++;
}
}
return $new_settings;
}
add_filter('woocommerce_general_settings'、'general_settings_shop_phone');
功能常规设置商店电话($settings){
$key=0;
foreach($设置为$值){
$new_设置[$key]=$value;
$key++;
//在“存储地址”部分的邮政编码后面插入数组
如果($values['id']='woocommerce\u store\u postcode'){
$new_设置[$key]=数组(
“title”=>(电话号码),
'desc'=>u u;('您的业务办公室的可选电话号码'),
'id'=>'woocommerce\u store\u phone',/',
'类型'=>'文本',
'desc_tip'=>真、//或假
);
$key++;
}
}
返回$new_设置;
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中
经过测试和工作…您将获得如下结果:
LoicTheAztec代码运行良好,但我想找到一个不循环的解决方案。以下是我的版本:
function add_phone_number_settings($settings) {
// Search array for the id you want
$key = array_search('woocommerce_store_postcode', array_column($settings, 'id')) + 1;
$custom_setting[] = array(
'title' => __('Phone Number'),
'desc' => __("Enter shop phone number"),
'id' => 'woocommerce_store_phone_number',
'default' => '',
'type' => 'text',
'desc_tip' => true,
);
// Merge with existing settings at the specified index
$new_settings = array_merge(array_slice($settings, 0, $key), $custom_setting, array_slice($settings, $key));
return $new_settings;
}
add_filter('woocommerce_general_settings', 'add_phone_number_settings');
这真是一种享受!谢谢你简单透彻的回答。
add_filter('woocommerce_general_settings', 'general_settings_shop_phone');
function general_settings_shop_phone($settings) {
$key = 0;
foreach( $settings as $values ){
$new_settings[$key] = $values;
$key++;
// Inserting array just after the post code in "Store Address" section
if($values['id'] == 'woocommerce_store_postcode'){
$new_settings[$key] = array(
'title' => __('Phone Number'),
'desc' => __('Optional phone number of your business office'),
'id' => 'woocommerce_store_phone', // <= The field ID (important)
'default' => '',
'type' => 'text',
'desc_tip' => true, // or false
);
$key++;
}
}
return $new_settings;
}
function add_phone_number_settings($settings) {
// Search array for the id you want
$key = array_search('woocommerce_store_postcode', array_column($settings, 'id')) + 1;
$custom_setting[] = array(
'title' => __('Phone Number'),
'desc' => __("Enter shop phone number"),
'id' => 'woocommerce_store_phone_number',
'default' => '',
'type' => 'text',
'desc_tip' => true,
);
// Merge with existing settings at the specified index
$new_settings = array_merge(array_slice($settings, 0, $key), $custom_setting, array_slice($settings, $key));
return $new_settings;
}
add_filter('woocommerce_general_settings', 'add_phone_number_settings');