Support

Account

Home Forums Backend Issues (wp-admin) Programmatically create field group on options page

Solved

Programmatically create field group on options page

  • I have trouble programmatically creating field group for specific options page.
    I have created options page with code and this works ok. Then I went to create field group by clicking options and I have placed a repeater field with image for that options page.
    This worked ok, so I went to Tools, selected the field group I just created and clicked “Generate export code”. The code I got I placed inside register_acf_fields function I am showing you, then I have commented out this code and deleted previously created field group. But when I remove comments so that I can check if this class works, I got nothing but options page is showing all right.
    What did I do wrong? It doesn’t work with add_action(‘acf/init’…

    class mypictures {
    
        public function __construct()   {
            add_action('init', array( $this, 'register_logo_options_page'));
            add_action('init', array( $this, 'register_acf_fields'));
        }
    
        public function register_logo_options_page()    {
    
            if( function_exists('acf_add_options_page') ) {
    
                $option_page = acf_add_options_page(array(
                    'page_title'    => 'Pictures',
                    'menu_title'    => 'Pictures',
                    'menu_slug'     => 'mypictures',
                    'capability'    => 'edit_posts',
                    'redirect'  => false
                    ));
    
            }
    
        }
    
        public function register_acf_fields ()  {
    /*      var_dump('asdf');
            die();*/
    
            if( function_exists('acf_add_local_field_group') ):
    
                acf_add_local_field_group(array (
                    'key' => 'group_582d62b7af067',
                    'title' => 'Pictures',
                    'fields' => array (
                        array (
                            'key' => 'field_582d62caed7d7',
                            'label' => 'Pictures',
                            'name' => 'pictures',
                            'type' => 'repeater',
                            'required' => 0,
                            'conditional_logic' => 0,
                            'wrapper' => array (
                                'width' => '',
                                'class' => '',
                                'id' => '',
                                ),
                            'collapsed' => '',
                            'min' => '',
                            'max' => '',
                            'layout' => 'table',
                            'button_label' => 'Add Row',
                            'sub_fields' => array (
                                array (
                                    'key' => 'field_582d62e3ed7d8',
                                    'label' => 'logo',
                                    'name' => 'logo',
                                    'type' => 'image',
                                    'instructions' => '',
                                    'required' => 0,
                                    'conditional_logic' => 0,
                                    'wrapper' => array (
                                        'width' => '',
                                        'class' => '',
                                        'id' => '',
                                        ),
                                    'return_format' => 'array',
                                    'preview_size' => 'medium',
                                    'library' => 'all',
                                    'min_width' => '',
                                    'min_height' => '',
                                    'min_size' => '',
                                    'max_width' => '',
                                    'max_height' => '',
                                    'max_size' => '',
                                    'mime_types' => '',
                                    ),
                                ),
                            ),
                        ),
                    'location' => array (
                        array (
                            array (
                                'param' => 'options_page',
                                'operator' => '==',
                                'value' => 'pictures',
                                ),
                            ),
                        ),
                    'menu_order' => 0,
                    'position' => 'normal',
                    'style' => 'default',
                    'label_placement' => 'top',
                    'instruction_placement' => 'label',
                    'hide_on_screen' => '',
                    'active' => 1,
                    'description' => '',
                    ));
    
            endif;
    
        }
    
    }
    
    $mypictures = new mypictures();
  • The value for the location rule should be the slug of the options page ‘mypictures` in this case.

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

The topic ‘Programmatically create field group on options page’ is closed to new replies.