禁用在Woocommerce中编辑特定的管理员自定义字段

问题描述 投票:1回答:1

我使用woocommerce_wp_text_input()函数在Inventory选项卡中为我的Woocommerce产品提供了一些自定义字段。我想添加一个自定义文本字段,仅用于显示值以供参考。

我想默认锁定文本字段,以便不能在其中写入内容。

可能吗?

php wordpress woocommerce admin custom-fields
1个回答
1
投票

是的,可以使用以下命令在字段arguments数组中添加as custom_attributes readonly属性:

'custom_attributes' => array('readonly' => 'readonly'),

所以你的代码就像:

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

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

enter image description here


更新:添加复选框以启用只读字段:

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'] ) );
        }
    }
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

复选框已禁用(字段不是只读):

enter image description here

复选框已启用(字段是只读的):

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.