Support

Account

Home Forums Feature Requests Generate fields solely from PHP

Solved

Generate fields solely from PHP

  • It’s possible that this is already a feature, and I’ve just missed how to do it.

    I’d like to be able to generate field groups and custom fields completely from PHP, without having to use the (fantastic) ACF admin screens. I build custom sites with ~10-100 field groups, each with a common prefix, and it would greatly improve my workflow if I could copy and paste field groups and fields in code and quickly build them without having to click through all the screens and options. Similar to how you would create custom fields with a class like Custom Metaboxes and Fields for WordPress.

    I know that I can build the fields, and then export to PHP code, which would look like this:

    if(function_exists("register_field_group"))
    {
    	register_field_group(array (
    		'id' => 'acf_home-media-logo-carousel',
    		'title' => 'Home: Media Logo Carousel',
    		'fields' => array (
    			array (
    				'key' => 'field_523115d7c4906',
    				'label' => 'Media Logos',
    				'name' => '_prefix_media_logos',
    				'type' => 'gallery',
    				'instructions' => 'Add logos of media outlets for the homepage carousel',
    				'preview_size' => 'thumbnail',
    				'library' => 'all',
    			),
    		),
    		'location' => array (
    			array (
    				array (
    					'param' => 'page_type',
    					'operator' => '==',
    					'value' => 'front_page',
    					'order_no' => 0,
    					'group_no' => 0,
    				),
    			),
    		),
    		'options' => array (
    			'position' => 'normal',
    			'layout' => 'default',
    			'hide_on_screen' => array (
    			),
    		),
    		'menu_order' => 0,
    	));
    }

    The only issue I see here is generating the unique key for each field:

    'key' => 'field_523115d7c4906'

    Is there currently a way for me to create my fields in PHP and have the field keys generated? Is this not a problem at all and the keys will be automatically generated? If not, would it be possible to add this as a possible workflow for ACF, for power users/developers who’d like to configure their fields solely from code?

    Thanks!

  • Hi @jjeaton

    The field key must be fixed in your code and cannot be created dynamically. It is the link between a saved value and the field that saved it. It is like an ID in the database row.

    You will need to generate this yourself and paste it in your code.

    Perhaps start at field_1, and increment the number for each field?

  • OK, so I can set the key field myself and it can be whatever I want it to be as long as it’s unique in the database? That’s perfect. Thanks!

  • I ran into this problem too, so I went over to w3schools and used their uniqid generator to generate a unique ID for me. I then just pasted that, along with field_ into my code. Hopefully this hint saves somebody some time.

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

The topic ‘Generate fields solely from PHP’ is closed to new replies.