我正在为Worpdress / WooCommerce创建一个插件.我已经完成了所有的工作,现在我想在woocommerce API设置中添加一个选项标签,就像在这个
instruction tutorial中一样.
这是我的代码:
add_filter( 'woocommerce_get_sections_api', 'some_function_to_add_tab' );
function some_function_to_add_tab( $sections ) {
$sections['some_settings'] = __( 'Some Settings', 'text-domain' );
return $sections;
}
add_filter( 'woocommerce_get_settings_api', 'some_tab_settings', 10, 2 );
function some_tab_settings( $settings, $current_section ) {
if ($current_section == 'some_settings') {
echo "settings here";
} else {
return $settings;
}
}
但我收到一些错误:
Warning: Missing argument 2 for some_tab_settings() in C:\OpenServer\domains\wp-dev\wp-content\plugins\some-plugin\some_plugin.php on line 30
Notice: Undefined variable: current_section in C:\OpenServer\domains\wp-dev\wp-content\plugins\some-plugin\some_plugin.php on line 31
相关:
add_filter( ‘woocommerce_get_settings_api’, ‘some_tab_settings’, 10, 2 ); ==> Line: 30
function some_tab_settings( $settings, $current_section ) { ==> Line: 31
我怎样才能做到这一点?
最佳答案 首先看一下
WC for WooCommerce API Settings和
this useful only tutorial(2016)的核心源代码,我已经找到了这个主题.这是代码:
// creating a new sub tab in API settings
add_filter( 'woocommerce_get_sections_api','add_subtab' );
function add_subtab( $settings_tabs ) {
$settings_tabs['custom_settings'] = __( 'Custom Settings', 'woocommerce-custom-settings-tab' );
return $settings_tabs;
}
// adding settings (HTML Form)
add_filter( 'woocommerce_get_settings_api', 'add_subtab_settings', 10, 2 );
function add_subtab_settings( $settings ) {
$current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
if ( $current_section == 'custom_settings' ) {
$custom_settings = array();
$custom_settings[] = array( 'name' => __( 'Custom Settings', 'text-domain' ),
'type' => 'title',
'desc' => __( 'The following options are used to ...', 'text-domain' ),
'id' => 'custom_settings'
);
$custom_settings[] = array(
'name' => __( 'Field 1', 'text-domain' ),
'id' => 'field_one',
'type' => 'text',
'default' => get_option('field_one'),
);
$custom_settings[] = array( 'type' => 'sectionend', 'id' => 'test-options' );
return $custom_settings;
} else {
// If not, return the standard settings
return $settings;
}
}
它用于在woocommerce设置选项卡API中创建新选项卡和子选项卡.第二个函数显示HTML字段,您可以在其中编辑和保存设置.
更新:使用新设置:
您现在只需使用新创建的设置,就像使用任何其他WordPress / WooCommerce设置一样,通过get_option()函数和设置的已定义ID(请参阅下面的参考资料).
例如,如果您的设置数组ID是’custom_settings’,您将使用get_option(‘custom_settings’).有关向WooCommerce添加设置的更多具体信息,请查看wooThemes: Settings API.可以使用echo var_dump();功能查看输出数据:
$my_data = get_option( 'custom_settings' );
echo var_dump( $my_data );
参考:
> wooThemes: Adding a Section to a Settings Tab
> wooThemes: Settings API