• Resolved jeanius-design

    (@jeanius-design)


    Hi there,

    How can I access the attachment ID on a custom action after submit? For other fields, I’m using something like this:

    ‘notes’ => get_field(‘notes’)

    but if I try that on a file upload field, it returns nothing. This line is blank:

    ‘file’ => get_field(‘file’)

    I want to use the file in a custom action. It has successfully uploaded the file to the media gallery, so I know it has submitted it alright – but not sure how to access it in the custom action.

    Thank you.

Viewing 1 replies (of 1 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    If you’re using a Custom Action, you can access your File field value using get_field('my_file') within the acfe/form/submit_my-action custom action submit hook (see documentation).

    Usage example:

    add_action('acfe/form/submit_my-action', 'my_custom_action_submit', 10, 2);
    function my_custom_action_submit($form, $action){
        
        // get file value
        $file = get_field('file');
        
        // log file
        acf_log($file);
        
        /*
         * Array(
         *     [ID] => 350
         *     [id] => 350
         *     [title] => 200.gif
         *     [filename] => 200.gif
         *     [filesize] => 834989
         *     ...
         * )
         */
        
    }

    Here is a Field Group + Form you can use which will work with the code example above.

    Field Group:

    add_action( 'acf/init', function(){
    
        acf_add_local_field_group( array(
            'key' => 'group_61a638bfcec10',
            'title' => 'My Form',
            'fields' => array(
                array(
                    'key' => 'field_664bfb05e1c85',
                    'label' => 'File',
                    'name' => 'file',
                    'aria-label' => '',
                    'type' => 'file',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'hide_field' => '',
                    'hide_label' => '',
                    'hide_instructions' => '',
                    'hide_required' => '',
                    'uploader' => '',
                    'return_format' => 'array',
                    'upload_folder' => '',
                    'multiple' => 0,
                    'max' => '',
                    'acfe_settings' => '',
                    'acfe_validate' => '',
                    'min_size' => '',
                    'max_size' => '',
                    'mime_types' => '',
                    'instruction_placement' => '',
                    'acfe_permissions' => '',
                    'library' => 'all',
                    'preview_style' => 'default',
                    'placeholder' => 'Select',
                    'button_label' => 'Add File',
                    'stylised_button' => 0,
                    'file_count' => 0,
                    'min' => '',
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'post',
                    ),
                ),
            ),
            'menu_order' => 0,
            'position' => 'normal',
            'style' => 'default',
            'label_placement' => 'left',
            'instruction_placement' => 'field',
            'hide_on_screen' => '',
            'active' => true,
            'description' => '',
            'show_in_rest' => 0,
        ));
    
    });

    ACFE Form:

    add_action( 'acfe/init', function(){
    
        acfe_register_form(array(
            'name' => 'my-form',
            'title' => 'My Form', 
            'field_groups' => array(
                'group_61a638bfcec10',
            ),
            'actions' => array(
                array(
                    'action' => 'custom',
                    'name' => 'my-action',
                ),
            ),
        ));
        
    });

    You can simply display it with [acfe_form name="my-form"] shortcode, or acfe_form('my-form') PHP code.

    Hope it helps!

    Have a nice day!

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Access File ID/Attachment ID after submit’ is closed to new replies.