You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从functions.php夺回WordPress新增产品自定义字段下拉控制权

修复WooCommerce产品自定义字段下拉菜单并添加自定义选项

方法一:重置过滤器钩子(优先尝试)

插件大概率篡改了WooCommerce控制自定义字段下拉选项的过滤器,停用后未清理回调。直接重置该过滤器并添加自定义选项:

将以下代码添加到主题(或子主题)的functions.php文件中:

add_action( 'admin_init', 'reset_woocommerce_custom_field_dropdown' );
function reset_woocommerce_custom_field_dropdown() {
    // 移除所有关联到该过滤器的第三方回调
    remove_all_filters( 'woocommerce_product_custom_field_options' );
    
    // 重新添加默认选项+自定义字段
    add_filter( 'woocommerce_product_custom_field_options', 'customize_product_custom_field_list' );
}

function customize_product_custom_field_list( $options ) {
    // 重置为WooCommerce默认选项,同时插入自定义字段
    $options = array(
        '' => __( '选择字段', 'woocommerce' ),
        '_sku' => __( 'SKU', 'woocommerce' ),
        '_price' => __( '价格', 'woocommerce' ),
        '_stock' => __( '库存数量', 'woocommerce' ),
        // 下方添加你的自定义字段,格式:字段键名 => 显示名称
        '_my_custom_field' => __( '我的自定义字段', 'your-text-domain' ),
        '_another_custom_field' => __( '第二个自定义字段', 'your-text-domain' ),
    );
    return $options;
}

方法二:完全重写自定义字段 metabox(方法一无效时使用)

如果插件直接修改了自定义字段的 metabox 结构,需要重新注册 metabox 并自定义渲染逻辑:

// 替换默认的自定义字段 metabox
add_action( 'add_meta_boxes', 're_register_product_custom_fields_metabox', 999 );
function re_register_product_custom_fields_metabox() {
    // 移除原有 metabox
    remove_meta_box( 'woocommerce-product-custom-fields', 'product', 'side' );
    
    // 重新注册自定义 metabox
    add_meta_box(
        'woocommerce-product-custom-fields',
        __( '自定义字段', 'woocommerce' ),
        'render_custom_product_custom_fields',
        'product',
        'side',
        'default'
    );
}

// 渲染自定义字段的UI
function render_custom_product_custom_fields( $post ) {
    ?>
    <div class="product_custom_fields">
        <p class="form-field">
            <label for="add_custom_field"><?php esc_html_e( '添加新自定义字段', 'woocommerce' ); ?></label>
            <select id="add_custom_field" name="add_custom_field">
                <option value=""><?php esc_html_e( '选择字段', 'woocommerce' ); ?></option>
                <option value="_sku"><?php esc_html_e( 'SKU', 'woocommerce' ); ?></option>
                <option value="_price"><?php esc_html_e( '价格', 'woocommerce' ); ?></option>
                <option value="_stock"><?php esc_html_e( '库存数量', 'woocommerce' ); ?></option>
                <!-- 自定义字段选项 -->
                <option value="_my_custom_field"><?php esc_html_e( '我的自定义字段', 'your-text-domain' ); ?></option>
            </select>
            <button type="button" class="button add_custom_field_button"><?php esc_html_e( '添加', 'woocommerce' ); ?></button>
        </p>

        <!-- 已添加的自定义字段列表 -->
        <?php
        $saved_fields = get_post_meta( $post->ID, '_product_custom_fields', true );
        $saved_fields = is_array( $saved_fields ) ? $saved_fields : array();
        
        if ( ! empty( $saved_fields ) ) {
            foreach ( $saved_fields as $key => $field ) {
                ?>
                <p class="form-field custom-field-wrap">
                    <input type="text" name="product_custom_fields[<?php echo esc_attr( $key ); ?>][key]" value="<?php echo esc_attr( $field['key'] ); ?>" placeholder="<?php esc_attr_e( '字段键名', 'woocommerce' ); ?>" />
                    <input type="text" name="product_custom_fields[<?php echo esc_attr( $key ); ?>][value]" value="<?php echo esc_attr( $field['value'] ); ?>" placeholder="<?php esc_attr_e( '字段值', 'woocommerce' ); ?>" />
                    <button type="button" class="button remove_custom_field_button"><?php esc_html_e( '移除', 'woocommerce' ); ?></button>
                </p>
                <?php
            }
        }
        ?>
        <input type="hidden" name="custom_fields_nonce" value="<?php echo wp_create_nonce( 'custom_product_fields' ); ?>" />
    </div>
    <?php
}

// 保存自定义字段数据
add_action( 'save_post_product', 'save_custom_product_fields', 10, 2 );
function save_custom_product_fields( $post_id, $post ) {
    // 验证权限和nonce
    if ( ! isset( $_POST['custom_fields_nonce'] ) || ! wp_verify_nonce( $_POST['custom_fields_nonce'], 'custom_product_fields' ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( ! current_user_can( 'edit_product', $post_id ) ) {
        return;
    }

    // 处理保存的字段数据
    $fields = isset( $_POST['product_custom_fields'] ) ? array_filter( $_POST['product_custom_fields'] ) : array();
    update_post_meta( $post_id, '_product_custom_fields', $fields );
}

注意事项

  • 建议使用子主题的functions.php修改,避免主题更新导致代码丢失。
  • 修改前请备份网站文件和数据库,防止出现意外。

内容的提问来源于stack exchange,提问作者Jon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 21:20:07