Support

Account

Home Forums General Issues Generating fields via PHP within plugin

Solved

Generating fields via PHP within plugin

  • I generated fields for my custom post type plugin. They showed as expected in the cpt. Now I want the fields to register for my post type when I activate the plugin.

    I exported my fields to php and added them in my plugin. I then deleted the fields from the admin menu. When activating the plugin the function is successfully called. But when I open my cpt post, the fields do not show.

    I know they are not showing in the ACF backend, but I think they should show in my post. My plugin is namespaced, so I call the functions from global namespace, like \register_field_group. Any thoughts?

    add_action('acf/init', 'wcp_konfigurator_add_ACF_fields');
    
    function wcp_konfigurator_add_ACF_fields () {
    	if(function_exists('\register_field_group')) {
    		// Check if group is already registered
    		$field_groups = \api_acf_get_field_groups(array());
    
    		foreach ($field_groups as $group) {
    			if ($group['type'] === 'acf_konfigurator-produkt') {
    				return;
    			}
    		}
    		
    		// register group
    		\register_field_group(array (
    			'id' => 'acf_konfigurator-produkt',
    			'title' => 'Konfigurator Produkt',
    			'fields' => array (
    				array (
    					'key' => 'field_5a4e3b4ec2a2b',
    etc.
  • Looking at the ACF docs, you need to add the location array to your field registration and attach it to your custom post type:

    
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post', // add your post_type here
    	),
        ),
    ),
    

    I haven’t tested this but I think that should work.

    More here from the docs: https://www.advancedcustomfields.com/resources/register-fields-via-php/

  • Thank you, now I am a little embarassed 😀

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

The topic ‘Generating fields via PHP within plugin’ is closed to new replies.