【问题标题】:Wordpress / add several Inputs in own PluginWordpress / 在自己的插件中添加几个输入
【发布时间】:2018-07-24 14:15:39
【问题描述】:

在我的输入字段中,我可以将数据添加到“options.php”并显示它。但是如何通过单击按钮(例如:“添加新”)生成第二个/第三个/...输入字段并将这些数据保存在“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' );}

【问题讨论】:

    标签: php wordpress list input repeater


    【解决方案1】:

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

    CMB2 是用于构建元框、自定义字段、 以及让您大吃一惊的 WordPress 表单。

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

    <?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

    【讨论】:

    • 谢谢,这对我有用。以及如何在前端显示(在本例中为“标题”)?
    • @Bavaria 见Display the Metadata
    • 所以我尝试了这个,但它没有用。 code $text = get_post_meta(get_the_ID(), 'title', true ); // 回显元数据 echo esc_html( $text ); code
    • 我可以从一个字段中获取数据:“$my_plugin_group_title = my_plugin_get_option('test_text');”。但这仅适用于常规字段(使用“add_field”创建)。如何从组内的字段中获取数据(使用“add_group_field”创建)?
    • @Bavaria 请阅读documentation。一切都详细解释了,如果您有新问题,请在 StackOverflow 上提出新问题
    猜你喜欢
    • 2011-07-16
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2023-04-09
    • 2016-05-13
    • 1970-01-01
    • 2017-08-07
    • 2019-03-04
    相关资源
    最近更新 更多