在Woocommerce中禁用编辑特定的管理员自定义字段 [英] Disable editing specific admin custom fields in Woocommerce
问题描述
我的库存选项卡中使用
更新:添加复选框以启用只读字段:
add_action('woocommerce_product_options_stock_status','display_product_options_inventory_custom_fields',20);函数display_product_options_inventory_custom_fields(){全球$ post;回声'</div>< div class ="options_group">';;//新的分隔部分//复选框woocommerce_wp_checkbox(array('id'=>'_enable_readonly','标签'=>__('启用只读字段','woocommerce'),'description'=>__('启用某些字段为只读','woocommerce'),'desc_tip'=>真的,));//获取复选框的值$ checkbox = get_post_meta($ post-> ID,'_enable_readonly',true);//我们根据复选框有条件地将字段属性设置为只读"$ readonly =空($ checkbox)吗?'':array('readonly'=>'readonly');//文字栏位1(条件为唯读)woocommerce_wp_text_input(array('id'=>'_text_field_ro1','type'=>'文本','标签'=>__('只读字段1','woocommerce'),'占位符'=>__('占位符文本1','woocommerce'),'description'=>__('自定义说明1:您的解释.','woocommerce'),'desc_tip'=>真的,'custom_attributes'=>$ readonly,//启用只读));//文字栏位2(条件为唯读)woocommerce_wp_text_input(array('id'=>'_text_field_ro2','type'=>'文本','标签'=>__('只读字段2','woocommerce'),'占位符'=>__('占位符文本2','woocommerce'),'description'=>__('自定义说明2:您的解释.','woocommerce'),'desc_tip'=>真的,'custom_attributes'=>$ readonly,//启用只读));}add_action('woocommerce_process_product_meta','save_product_custom_fields');函数save_product_custom_fields($ post_id){//1.只读复选框$ readonly = isset($ _POST ['_ enable_readonly'])吗?esc_attr($ _POST ['_ enable_readonly']):'';update_post_meta($ post_id,'_enable_readonly',$ readonly);//2.只读字段:禁用只读时允许保存if(!isset($ _POST ['_ enable_readonly'])){//保存文本字段1的值if(isset($ _POST ['_ text_field_ro1'])){update_post_meta($ post_id,'_text_field_ro1',sanitize_text_field($ _POST ['_ text_field_ro1']));}//保存文本字段2的值if(isset($ _POST ['_ text_field_ro2'])){update_post_meta($ post_id,'_text_field_ro2',sanitize_text_field($ _POST ['_ text_field_ro2']));}}}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
复选框被禁用(字段不是只读的):
复选框已启用(字段为只读):
I have some custom fields for my Woocommerce products inside my Inventory Tab using woocommerce_wp_text_input() function. I would like to add one more custom text field just for displaying a value for reference.
I want to lock the textfield by default so as not to be able to write something in it.
Is it possible?
Yes it is possible adding the as custom_attributes the readonly property in the field arguments array using:
'custom_attributes' => array('readonly' => 'readonly'),
So your code will be like:
add_action( 'woocommerce_product_options_stock_status', 'display_product_options_inventory_custom_fields', 20 );
function display_product_options_inventory_custom_fields() {
global $post;
echo '</div><div class="options_group">'; // New separated section
// Text field (conditionally readonly)
woocommerce_wp_text_input( array(
'id' => '_text_field_ro',
'type' => 'text',
'label' => __( 'Read only field', 'woocommerce' ),
'placeholder' => __( 'placeholder text', 'woocommerce' ),
'description' => __( 'Custom description: your explanations.', 'woocommerce' ),
'desc_tip' => true,
'custom_attributes' => $readonly, // Enabling read only
) );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Update: Adding a checkbox to enable the readonly fields:
add_action( 'woocommerce_product_options_stock_status', 'display_product_options_inventory_custom_fields', 20 );
function display_product_options_inventory_custom_fields() {
global $post;
echo '</div><div class="options_group">'; // New separated section
// Checkbox
woocommerce_wp_checkbox( array(
'id' => '_enable_readonly',
'label' => __( 'Enable readonly fields', 'woocommerce' ),
'description' => __( 'Enable some fields to be readonly', 'woocommerce' ),
'desc_tip' => true,
));
// Get the checkbox value
$checkbox = get_post_meta( $post->ID, '_enable_readonly', true );
// We set the field attribute "readonly" conditionally based on the checkbox
$readonly = empty($checkbox) ? '' : array('readonly' => 'readonly');
// Text field 1 (conditionally readonly)
woocommerce_wp_text_input( array(
'id' => '_text_field_ro1',
'type' => 'text',
'label' => __( 'Read only field 1', 'woocommerce' ),
'placeholder' => __( 'placeholder text 1', 'woocommerce' ),
'description' => __( 'Custom description 1: your explanations.', 'woocommerce' ),
'desc_tip' => true,
'custom_attributes' => $readonly, // Enabling read only
) );
// Text field 2 (conditionally readonly)
woocommerce_wp_text_input( array(
'id' => '_text_field_ro2',
'type' => 'text',
'label' => __( 'Read only field 2', 'woocommerce' ),
'placeholder' => __( 'placeholder text 2', 'woocommerce' ),
'description' => __( 'Custom description 2: your explanations.', 'woocommerce' ),
'desc_tip' => true,
'custom_attributes' => $readonly, // Enabling read only
) );
}
add_action( 'woocommerce_process_product_meta', 'save_product_custom_fields' );
function save_product_custom_fields( $post_id ) {
// 1. readonly checkbox
$readonly = isset( $_POST['_enable_readonly'] ) ? esc_attr( $_POST['_enable_readonly'] ) : '';
update_post_meta( $post_id, '_enable_readonly', $readonly );
// 2. Readonly fields: allow saving when readonly is disabled
if( ! isset( $_POST['_enable_readonly'] ) ){
// Save text field 1 value
if( isset( $_POST['_text_field_ro1'] ) ){
update_post_meta( $post_id, '_text_field_ro1', sanitize_text_field( $_POST['_text_field_ro1'] ) );
}
// Save text field 2 value
if( isset( $_POST['_text_field_ro2'] ) ){
update_post_meta( $post_id, '_text_field_ro2', sanitize_text_field( $_POST['_text_field_ro2'] ) );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Checkbox is disabled (fields are not readonly):
Checkbox is enabled (fields are readonly):
这篇关于在Woocommerce中禁用编辑特定的管理员自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!