Wordpress / 在自己的插件中添加几个输入

Wordpress / add several Inputs in own Plugin

在我的输入字段中,我可以向 "options.php" 添加数据并显示它。但是我如何通过单击按钮(例如:"add new")生成 second/third/... 输入字段并将此数据保存在 "options.php" 中?

function ww_contact_new_page() {
?>
<form method="post" action="options.php">
    <?php settings_fields( 'ww-contact-settings-group' );
    do_settings_sections( 'ww-contact-settings-group' ); ?>
    <input type="text" name="ww_contact_name" placeholder="/impressum" value="<?php echo esc_attr( get_option('ww_contact_name') ); ?>" />
    <?php submit_button(); ?>
</form>
<?php}

/* Register */
function register_ww_contact_settings() {
register_setting( 'ww-contact-settings-group', 'ww_contact_name' );}

最好使用 CMB2 这样的框架来为您完成繁重的工作

CMB2 is a developer's toolkit for building metaboxes, custom fields, and forms for WordPress that will blow your mind.

您可以创建输入组并重复它们,或者创建具有重复输入的组

<?php

$group_field_id = $cmb->add_field(array(
    'id'          => 'wiki_test_repeat_group',
    'type'        => 'group',
    'description' => __('Generates reusable form entries', 'cmb2'),
    'repeatable'  => false, 
    'options'     => array(
        'group_title'   => __('Entry {#}', 'cmb2'), 
        'add_button'    => __('Add Another Entry', 'cmb2'),
        'remove_button' => __('Remove Entry', 'cmb2'),
        'sortable'      => true,
    ),
));

$cmb->add_group_field($group_field_id, array(
    'name' => 'Entry Title',
    'id'   => 'title',
    'type' => 'text',
    'repeatable' => true,
));

另见 Using CMB to create an Admin Theme Options Page