Support

Account

Forum Replies Created

  • i try to modify these functions (get here)
    https://www.pearlbells.co.uk/insert-update-acf-image-repeater-field-programmatically/

    function uploadImages( $files ) {
     
        $imgAcfIds = array();
     
        foreach($files as $imgfile) {
            $filename = basename($imgfile);
            $upload_file = wp_upload_bits($filename, null, file_get_contents($imgfile));
            if (!$upload_file['error']) {
                $wp_filetype = wp_check_filetype($filename, null );
                $attachment = array(
                    'post_mime_type' => $wp_filetype['type'],
                    'post_parent' => 251,
                    'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
                    'post_content' => '',
                    'post_status' => 'inherit',
    				'post_author'   => 2,
    	'post_category' => array(9)
    	
                );
    			
    			
                $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 251 );
                
    			
                // get attachment id for acf field
     
                $imgAcfIds[] = array('documento' => $attachment_id);
                echo 'Attachment Id :'.$attachment_id .PHP_EOL;
                if (!is_wp_error($attachment_id)) {
                    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                    $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
                    wp_update_attachment_metadata( $attachment_id,  $attachment_data );
                }
            }
        }
        echo '<pre>';
        print_r($imgAcfIds);
    	$post_id = wp_insert_post( wp_slash($attachment) );
        update_field( 'field_5d78c5acf0ff3', $imgAcfIds , 251 );
    	
    }
    
        function readDataFromImageFolder() {
            
            $imageFolderName = 14;
            $base = dirname(__FILE__);
            $dirname = $base.'/images/'.$imageFolderName;
            $files = array();
            
            if (!file_exists($dirname)) {
                echo "The directory $dirname not exists.".PHP_EOL;
                exit;
            } else {
                echo "The directory $dirname exists.".PHP_EOL;
                $dh  = opendir( $dirname );
                
                while (false !== ($filename = readdir($dh))) {
                    if ($filename === '.' || $filename === '..') continue;
                    $files[] = $dirname.$filename;
                }
                uploadImages( $files );
            }
         
          //  echo '<pre>';
          //  print_r($files);die();
         
        }
    

    but don’t work my file field name is documento my file field key is field_5d78c5acf0ff3a

    thanks a lot

  • thank you for this, but i don’t undestand how i can integrate with
    wp_insert_post function.
    Thanks a lot

  • I solved the problem by myself.
    In the file class-acf-field-file.php
    remove:

    <a class="acf-icon -pencil dark" data-name="edit" href="#" title="<?php _e('Edit', 'acf'); ?>"></a>

    Thanks anyway.

  • Ok thank-you,can you kindly tell me, the name of the div, which I have to hide? Thanks.

  • ok, but what is the purpose of the pen button?
    is there a way to delete the button by acting on the php?

  • my idea is to create a document tree, with posts, inserting a file field, for download, but I can’t replace the documents already inserted. I can only delete them and insert the new file, without changing an entry already made (using the pencil icon). I hope I explained myself.

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