Support

Account

Home Forums Backend Issues (wp-admin) Create field type in theme (without plugin)

Solved

Create field type in theme (without plugin)

  • I’ve been able to create new field types by using the starter kit (https://github.com/AdvancedCustomFields/acf-field-type-template), but I have a situation where I need to create the new field type within a theme. I’ve tried porting the code over but I keep running into issues with object declarations. Has anybody done this before? Ideally I’d just have a PHP file for each field type and the JS would be handled in the theme (I’m enqueueing an admin script for the theme).

  • Can you give more details on the issues your having?

  • I think I got it figured out… I ended up with the following file structure:

    wp-content/
    ↳ themes/
       ↳ [theme]/
          ↳ acf/
             ↳ assets/
                ↳ css/
                   ↳ [field].css
                ↳ js/
                   ↳ [field].js
             ↳ fields/
                ↳ [field].php
             ↳ json/
             ↳ init.php

    In init.php I’m setting the acf-json directory, and also including my fields:

    add_action ( 'acf/include_field_types', 'acf_include_field_types' );
    function include_field_types ( $version = false ) {
      if ( $version == 5 ) {
        foreach ( ( array ) glob ( get_template_directory() . '/acf/fields/*.php' ) as $field ) {
          include $field;
        }
      }
    }

    Then, in the field class that extends acf_field, I just removed $settings from the __construct() call and defined them in the $this->settings variable:

    $this->settings = array (
      'version' => '1.0.0',
      'url' => get_template_directory_uri() . '/acf/',
      'path' => get_template_directory() . '/acf/'
    );

    So going forward, I should just need to create new PHP, CSS and JS files for each field type. Hope this helps someone!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Create field type in theme (without plugin)’ is closed to new replies.