最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom field - limit amount of photos uploaded per cpt post

matteradmin9PV0评论

On my site I have a front end form for users to create a post and upload images. It works fine however I cant seem to figure out how to limit the number of images per post.

The image attachments get saved to a custom field, so is there a way to limit the amount of values allowed to the custom field?

I also came across this post but was unable to get it to work.

current code in use for uploading files --

// THE PHOTO UPLOAD HERE
if(isset($_POST["savepics2"])) {

$attachments = get_children( array( 'post_parent' => $v_Id ) );
$count = count( $attachments );
if ($count == 25) {
echo 'limit reached';
} else {


if (!empty($_FILES['vidPix']['tmp_name'][0])) {
                    $i = 1;
                    $files = $_FILES['vidPix'];
                    foreach ($files['name'] as $key => $value) {
                        if ($files['name'][$key]) {
                            $file = array(
                                'name' => $files['name'][$key],
                                'type' => $files['type'][$key],
                                'tmp_name' => $files['tmp_name'][$key],
                                'error' => $files['error'][$key],
                                'size' => $files['size'][$key]
                            );
                            $_FILES = array("sight" . $i => $file);
add_filter( 'upload_dir', 'wpse_141088_upload_dir' );
add_filter('intermediate_image_sizes_advanced', 'no_image_resizing');

                        $mfile =  wp_handle_upload($files, $upload_overrides );             

                            $newvidPix = sight("sight" . $i, $v_Id);
remove_filter( 'upload_dir', 'wpse_141088_upload_dir' );
remove_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
// Convert the image to PNG and delete the old image.
attachment_to_png( $newvidPix );

                            if ($i == 1) {
                                update_post_meta($v_Id, '_thumbnail_id', $newvidPix);
                            }
                                add_post_meta($v_Id, 'vid_pix', $newvidPix, false);
                        }
                        $i++;
                    }
                }
        }
}

Any ideas? thanks.

On my site I have a front end form for users to create a post and upload images. It works fine however I cant seem to figure out how to limit the number of images per post.

The image attachments get saved to a custom field, so is there a way to limit the amount of values allowed to the custom field?

I also came across this post but was unable to get it to work.

current code in use for uploading files --

// THE PHOTO UPLOAD HERE
if(isset($_POST["savepics2"])) {

$attachments = get_children( array( 'post_parent' => $v_Id ) );
$count = count( $attachments );
if ($count == 25) {
echo 'limit reached';
} else {


if (!empty($_FILES['vidPix']['tmp_name'][0])) {
                    $i = 1;
                    $files = $_FILES['vidPix'];
                    foreach ($files['name'] as $key => $value) {
                        if ($files['name'][$key]) {
                            $file = array(
                                'name' => $files['name'][$key],
                                'type' => $files['type'][$key],
                                'tmp_name' => $files['tmp_name'][$key],
                                'error' => $files['error'][$key],
                                'size' => $files['size'][$key]
                            );
                            $_FILES = array("sight" . $i => $file);
add_filter( 'upload_dir', 'wpse_141088_upload_dir' );
add_filter('intermediate_image_sizes_advanced', 'no_image_resizing');

                        $mfile =  wp_handle_upload($files, $upload_overrides );             

                            $newvidPix = sight("sight" . $i, $v_Id);
remove_filter( 'upload_dir', 'wpse_141088_upload_dir' );
remove_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
// Convert the image to PNG and delete the old image.
attachment_to_png( $newvidPix );

                            if ($i == 1) {
                                update_post_meta($v_Id, '_thumbnail_id', $newvidPix);
                            }
                                add_post_meta($v_Id, 'vid_pix', $newvidPix, false);
                        }
                        $i++;
                    }
                }
        }
}

Any ideas? thanks.

Share Improve this question edited Dec 3, 2018 at 18:45 730wavy asked Nov 13, 2018 at 15:03 730wavy730wavy 1931 gold badge13 silver badges45 bronze badges 2
  • Can you update the question with your code written so far? – middlelady Commented Nov 13, 2018 at 17:08
  • sorry about the delay. I've updated the question with code. – 730wavy Commented Dec 3, 2018 at 18:46
Add a comment  | 

1 Answer 1

Reset to default 1

You can do it like so — refer to the // {comment}:

if (isset($_POST["savepics2"])) {
    // Set max number of files allowed.
    $max_files = 25;

    $attachments = get_children( array( 'post_parent' => $v_Id ) );
    $count = count( $attachments );

    // Check if limit already reached.
    if ($count >= $max_files) {
        echo 'limit reached';
    // If not, then upload the files.
    } else {
        if (!empty($_FILES['vidPix']['tmp_name'][0])) {
            $i = 1;
            $files = $_FILES['vidPix'];

            foreach ($files['name'] as $key => $value) {
                // Check if limit already reached.
                if ( $count >= $max_files ) {
                    echo 'limit reached';
                    break;
                }

                // If not, then upload next file.
                if ($files['name'][$key]) {
                    ...your code here...
                }

                $i++;
                $count++; // increment the count
            }
        }
    }
}
Post a comment

comment list (0)

  1. No comments so far