skip to Main Content

I have created custom field type following this documentation https://www.advancedcustomfields.com/resources/creating-a-new-field-type/ and I have below fields:

  • YouTube ID(input type text)
  • Submit Button(input type submit)
  • Title(input type text)
  • Description(input type text)
  • Thumbnail URL(input type text)
  • Upload Date(input type text)
  • Duration minute(input type number)
  • Duration seconds(input type number)

I have saw this question, Advanced Custom Fields – Custom Field Type with multiple inputs and ACF: Creating Custom Field Type store two values. I am expecting that I could do it this way when setting the fields.

<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'])?>"
/>
...

But this give me an error:
enter image description here

Can anyone guide me on this, below is my full code:

    <?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();

2

Answers


  1. $field['name'] is a string. Thus, doing $field['name']['foo'] or the equivalent 'string'['foo'] is illegal in PHP and will raise the error. You may want to consider something like this instead:

    <input type="text"
      name="<?php echo esc_attr($field['name']); ?>"
      value="<?php echo esc_attr($field['value']); ?>"
    />
    
    Login or Signup to reply.
  2. It looks like the issue is with your use of $field[‘name’]. The name attribute of the input should be set to the field key, which in your case is ‘youtube_id’.

    So instead of:

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

    Use:

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

    Also, you are currently using $field[‘name’][‘youtube-id’] as both the name and value attributes for the input field. Instead, you should set the name attribute to ‘youtube_id’ and the value attribute to $field[‘value’]. So the corrected code would be:

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

    Similarly, you will need to update the name attributes for the other input fields, like this:

    <input type="text" name="title" value="<?php echo esc_attr($field['value']['title']) ?>" />
    <input type="text" name="description" value="<?php echo esc_attr($field['value']['description']) ?>" />
    <input type="text" name="thumbnail_url" value="<?php echo esc_attr($field['value']['thumbnail_url']) ?>" />
    <input type="text" name="upload_date" value="<?php echo esc_attr($field['value']['upload_date']) ?>" />
    <input type="number" name="duration_minutes" value="<?php echo esc_attr($field['value']['duration_minutes']) ?>" />
    <input type="number" name="duration_seconds" value="<?php echo esc_attr($field['value']['duration_seconds']) ?>" />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search