ACF 创建多字段,自定义字段类型

问题描述 投票:0回答:1

我已经按照本文档创建了自定义字段类型https://www.advancedcustomfields.com/resources/creating-a-new-field-type/我有以下字段:

  • YouTube ID(输入类型文本)
  • 提交按钮(输入类型提交)
  • 标题(输入类型文字)
  • 描述(输入类型文本)
  • 缩略图网址(输入类型文本)
  • 上传日期(输入类型文本)
  • 持续时间分钟(输入类型数字)
  • Duration seconds(input type number)

我看到了这个问题,Advanced Custom Fields – Custom Field Type with multiple inputsACF: Creating Custom Field Type store two values。我希望在设置字段时可以这样做。

<input type="text"
  name="<?php echo esc_attr($field['name']['youtube-id'])?>"
  value="<?php echo esc_attr($field['value']['youtube-id'])?>"
/>
 <input type="text"
  name="<?php echo esc_attr($field['name']['youtube-title'])?>"
  value="<?php echo esc_attr($field['value']['youtube-title'])?>"
/>
...

但这给了我一个错误:

谁能指导我,下面是我的完整代码:

    <?php
/**
 * Defines the custom field type class.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * getgoally_acf_field_fetch_youtube_detail class.
 */
class getgoally_acf_field_fetch_youtube_detail extends \acf_field {
    /**
     * Controls field type visibilty in REST requests.
     *
     * @var bool
     */
    public $show_in_rest = true;

    /**
     * Environment values relating to the theme or plugin.
     *
     * @var array $env Plugin or theme context such as 'url' and 'version'.
     */
    private $env;

    /**
     * Constructor.
     */
    public function __construct() {
        /**
         * Field type reference used in PHP and JS code.
         *
         * No spaces. Underscores allowed.
         */
        $this->name = 'fetch_youtube_detail';

        /**
         * Field type label.
         *
         * For public-facing UI. May contain spaces.
         */
        $this->label = __( 'Fetch YouTube Detail', 'plugin-or-theme-name' );

        /**
         * The category the field appears within in the field type picker.
         */
        $this->category = 'basic'; // basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME

        /**
         * Defaults for your custom user-facing settings for this field type.
         */
        $this->defaults = array(
        );

        /**
         * Strings used in JavaScript code.
         *
         * Allows JS strings to be translated in PHP and loaded in JS via:
         *
         * ```js
         * const errorMessage = acf._e("fetch_youtube_detail", "error");
         * ```
         */
        $this->l10n = array(
            'error' => __( 'Error! Please enter a higher value', 'plugin-or-theme-name' ),
        );

        $this->env = array(
            // 'url'     => site_url( str_replace( ABSPATH, '', __DIR__ ) ), // URL to the acf-fetch-youtube-detail directory.
            'url'     => plugin_dir_url( __FILE__ ),
            'version' => '1.0', // Replace this with your theme or plugin version constant.
        );
        parent::__construct();
        
    }

    /**
     * Settings to display when users configure a field of this type.
     *
     * These settings appear on the ACF “Edit Field Group” admin page when
     * setting up the field.
     *
     * @param array $field
     * @return void
     */
    public function render_field_settings( $field ) {
        /*
         * Repeat for each setting you wish to display for this field type.
         */
        acf_render_field_setting(
            $field,
            array(
                'type'          => 'text',
                'name'          => 'fetch_youtube_detail',
            )
         
        );

        // To render field settings on other tabs in ACF 6.0+:
        // https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/#moving-field-setting
    }

    /**
     * HTML content to show when a publisher edits the field on the edit screen.
     *
     * @param array $field The field settings and values.
     * @return void
     */
    public function render_field( $field ) {
        // Debug output to show what field data is available.
        // echo '<pre>';
        // print_r( $field );
        // echo '</pre>';

        // Display an input field that uses the 'font_size' setting.
         
        ?>
            <div class="fields">
                <div class="field">
                    <div class="label">
                        <label for="youtube-id">YouTube ID</label>
                    </div>
                    <div class="input">
                        <input type="text"
                          name="<?php echo esc_attr($field['name']['youtube-id'])?>"
                          value="<?php echo esc_attr($field['name']['youtube-id'])?>"
                        />
                        <input type="submit" value="Get YouTube Detail" name="get-youtube-detail"> 
                    
                    </div>
                </div>
                <div class="field">
                    <div class="label">
                        <label for="title">Title</label>
                    </div>
                    <div class="input">
                        <input type="text" name="title">
                    </div>
                </div>
                <div class="field">
                    <div class="label">
                        <label for="description">Description</label>
                    </div>
                    <div class="input">
                        <input type="text" name="description">
                    </div>
                </div>
                <div class="field">
                    <div class="label">
                        <label for="thumbnail-url">Thumbnail Url</label>
                    </div>
                    <div class="input">
                        <input type="text" name="thumbnail-url">
                    </div>
                </div>
                <div class="field">
                    <div class="label">
                        <label for="upload-date">Upload Date</label>
                    </div>
                    <div class="input">
                        <input type="text" name="upload-date">
                    </div>
                </div>
                <div class="field">
                    <div class="label">
                        <label>Duration</label>
                    </div>
                    <div class="input">
                        <div class="fields">
                             <div class="field">
                                 <div class="label">
                                     <label for="minutes">Minutes</label>
                                 </div>
                                 <div class="input">
                                     <input type="number" name="minutes">
                                 </div>
                             </div>
                             <div class="field">
                                 <div class="label">
                                     <label for="seconds">Seconds</label>
                                 </div>
                                 <div class="input">
                                     <input type="number" name="seconds">
                                 </div>
                             </div> 
                        </div>
                    </div>
                </div>
            </div>
        <?php
            $field = get_field('fetch_youtube_detail');
            echo 'TEST';
            echo '<pre>';
            print_r($field);
            echo '</pre>';
    }

    /**
     * Enqueues CSS and JavaScript needed by HTML in the render_field() method.
     *
     * Callback for admin_enqueue_script.
     *
     * @return void
     */
    public function input_admin_enqueue_scripts() {
        $url     = trailingslashit( $this->env['url'] );
        $version = $this->env['version'];
        echo 'URL '.$url;  
        wp_register_script(
            'getgoally-fetch-youtube-detail',
            "{$url}assets/js/field.js",
            array( 'acf-input' ),
            $version
        );

        wp_register_style(
            'getgoally-fetch-youtube-detail',
            "{$url}assets/css/field.css",
            array( 'acf-input' ),
            $version
        );

        wp_enqueue_script( 'getgoally-fetch-youtube-detail' );
        wp_enqueue_style( 'getgoally-fetch-youtube-detail' );
    }
    
}


// $obj = new getgoally_acf_field_fetch_youtube_detail();
php wordpress advanced-custom-fields
1个回答
0
投票

$field['name']
是一个字符串。因此,执行
$field['name']['foo']
或等效的
'string'['foo']
在 PHP 中是非法的,并且会引发错误。你可能想考虑这样的事情:

<input type="text"
  name="<?php echo esc_attr($field['name']); ?>"
  value="<?php echo esc_attr($field['value']); ?>"
/>
© www.soinside.com 2019 - 2024. All rights reserved.