概要
woocommerce_settings_api_form_fields_$THIS->ID
フィルタは、WooCommerceの設定ページにカスタムフィールドを追加する際に利用されます。このフィルタを使用することで、特定の設定セクションに対して新しい入力フィールドやオプションを追加することができます。具体的には、以下のような機能を実装する際に役立ちます。
- 支払いゲートウェイの設定項目の追加
- 配送オプションの拡張
- カスタムメール設定の追加
- プロモーションやクーポンコードの設定欄の実装
- 商品の詳細設定をユーザーが指定できるようにする
- ウェブサイトのカスタマイズオプションを提供する
構文
add_filter( 'woocommerce_settings_api_form_fields_{this->id}', 'your_custom_function' );
パラメータ
$this->id
: WooCommerceの特定の設定セクションのID。
戻り値
- フィルタ関数は、WooCommerceの設定フォームフィールドの配列を返す。
使用可能なバージョン
- WooCommerce: 2.0 以降
- WordPress: 3.0 以降
サンプルコード
サンプルコード 1
add_filter( 'woocommerce_settings_api_form_fields_payment_gateways', 'add_custom_gateway_settings' );
function add_custom_gateway_settings( $settings ) {
$settings['custom_option'] = array(
'title' => __( 'Custom Option', 'woocommerce' ),
'type' => 'checkbox',
'description' => __( 'Enable this option for custom functionality.', 'woocommerce' ),
'default' => 'no',
'desc_tip' => true,
);
return $settings;
}
このコードは、支払いゲートウェイの設定に「カスタムオプション」というチェックボックスフィールドを追加します。
サンプルコード 2
add_filter( 'woocommerce_settings_api_form_fields_shipping', 'add_custom_shipping_settings' );
function add_custom_shipping_settings( $settings ) {
$settings['custom_shipping_option'] = array(
'title' => __( 'Custom Shipping Option', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Enter your custom shipping option.', 'woocommerce' ),
'default' => '',
);
return $settings;
}
こちらは、配送設定画面にカスタムテキストフィールドを追加する例です。
サンプルコード 3
add_filter( 'woocommerce_settings_api_form_fields_general', 'add_custom_general_settings' );
function add_custom_general_settings( $settings ) {
$settings['custom_general_setting'] = array(
'title' => __( 'Custom General Setting', 'woocommerce' ),
'type' => 'select',
'options' => array(
'option1' => __( 'Option 1', 'woocommerce' ),
'option2' => __( 'Option 2', 'woocommerce' ),
),
'default' => 'option1',
);
return $settings;
}
このコードは、一般設定ページにセレクトボックスを追加するものです。
サンプルコード 4
add_filter( 'woocommerce_settings_api_form_fields_emails', 'add_custom_email_settings' );
function add_custom_email_settings( $settings ) {
$settings['custom_email_option'] = array(
'title' => __( 'Custom Email Option', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Custom message for emails.', 'woocommerce' ),
'default' => '',
);
return $settings;
}
このサンプルコードは、WooCommerceのメール設定にテキストエリアを追加するものです。
サンプルコード 5
add_filter( 'woocommerce_settings_api_form_fields_products', 'add_custom_product_settings' );
function add_custom_product_settings( $settings ) {
$settings['custom_product_field'] = array(
'title' => __( 'Custom Product Field', 'woocommerce' ),
'type' => 'number',
'description' => __( 'Set a custom number for products.', 'woocommerce' ),
'default' => '0',
);
return $settings;
}
こちらは、商品設定にカスタム数値フィールドを追加する例です。
この関数のアクションでの使用可能性
アクション | 使用可否 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |