WordPress设置API可重复字段

时间:2020-05-06 作者:trunks

我正在试图找到一种方法,在我制作的自定义管理页面中添加可重复的字段。

代码:

  /*WordPress Menus API.*/
    function add_new_menu_items()
    {
        add_menu_page(
            "Theme Options", 
            "Theme Options",
            "manage_options",
            "theme-options", 
            "theme_options_page", 
            "", 
            100 
        );

    }

    function theme_options_page()
    {
        ?>
        <?php settings_errors(); ?>
            <div class="wrap">
            <div id="icon-options-general" class="icon32"></div>
            <h1>Theme Options</h1>
            <form method="post" action="options.php">
                <?php

                    settings_fields("header_section");

                    do_settings_sections("theme-options");

                    submit_button();

                ?>         
            </form>
        </div>
        <?php
    }

    add_action("admin_menu", "add_new_menu_items");



    function display_options()
    {
        add_settings_section("header_section", "Header Options", "display_header_options_content", "theme-options");

        add_settings_field("header_logo", "Logo Url", "display_logo_form_element", "theme-options", "header_section");
        add_settings_field("advertising_code", "Ads Code", "display_ads_form_element", "theme-options", "header_section");

        register_setting("header_section", "header_logo");
        register_setting("header_section", "advertising_code");
    }

    function display_header_options_content(){echo "The header of the theme";}
    function display_logo_form_element()
    {
        ?>
            <input type="text" name="header_logo" id="header_logo" value="<?php echo get_option(\'header_logo\'); ?>" />
        <?php
    }
    function display_ads_form_element()
    {
        ?>
            <input type="text" name="advertising_code" id="advertising_code" value="<?php echo get_option(\'advertising_code\'); ?>" />
        <?php
    }

    add_action("admin_init", "display_options");
有没有办法使advertising\\u代码输入字段可重复?

非常感谢。

2 个回复
SO网友:user141080

这不是最好的解决方案,但也许创建自己的解决方案对你有帮助。

function display_ads_form_element()
{
    // get the total number of "advertising codes" from the database
    // the default value is 1
    $codes  = array(\'1\' => \'\' );
    if( get_option(\'advertising_code\') !== FALSE ) {
        $codes = get_option(\'advertising_code\');
    }
    ?>

    <?php /// button to add the new field // ?>
    <input type="button" onClick="add_new_ad_code()" value="add new advertising code">

    <ul id="advertising_code_list" >

        <?php foreach($codes as $key => $code): ?>

            <?php /// create for every "advertising code" an input field // ?>
            <?php /// advertising_codes[key] => square brackets means the data will send as array  // ?>
            <?php /// data-listkey => is only an easy way that the js can detect the last key  // ?>
            <li><input type="text" name="advertising_code[<?php echo esc_attr( $key ); ?>]" value="<?php echo esc_attr( $code ); ?>" data-listkey="<?php echo esc_attr( $key ); ?>"/></li>

        <?php endforeach; ?>


    </ul>

    <script>

        function add_new_ad_code() {

            // detect the last li element
            const last_li = document.getElementById(\'advertising_code_list\').lastElementChild;

            // get the listKey from the input field
            const new_key = parseInt(last_li.childNodes[0].dataset.listkey) + 1;

            // create the new list tag
            const li_element = document.createElement(\'li\');

            // create the new input tag for the new advertising code
            const input_element = document.createElement(\'input\');

            // set the type
            input_element.type = "text";

            // set the name attribute
            input_element.name = "advertising_code[" + new_key +  "]";

            // set empty value
            input_element.value = "";

            // add the new key as data attribute
            input_element.dataset.listkey = new_key;

            // add the new advertising code to the list element
            li_element.appendChild(input_element);

            // add the list element to the list
            document.getElementById(\'advertising_code_list\').appendChild(li_element);
        }

    </script>

    <?php
}

screenshot

SO网友:Ejaz UL Haq

在页面顶部的PHP开始标记后添加,如
<?php $advertising_code = \'advertising_code\';
并在需要的地方调用它$advertising_code;

相关推荐

将序号添加到GravityForms窗体

我有多种表格。基于这些表单,我需要一个用前缀和序列号填充的隐藏字段。每个表单都有一个隐藏字段。该字段可以动态填充,参数为“uuid”。我有以下代码。。。对如何更改以下代码有何建议?这个代码段可以工作,但它使用了一个随机数(mt\\u rand),我更喜欢[prefix]-00001,[prefix]-00002,等等。此外,如果每个表单都有自己的前缀,那就更好了。但这超出了我的php技能。对于每种形式,最好是顺序的,例如:Form A 前缀为“prd”([前缀]-00001)prd-00001Form B