0

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.

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

1 Answer 1

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
            }
        }
    }
}
4
  • i havent tested yet but question, since the input accepts multiple files, if a scenario where lets say - user has already upload 15 photos. He tries selecting 15 more photos to upload, what happens then ?
    – 730wavy
    Commented Dec 13, 2018 at 17:08
  • Assuming the max files is 20, then only 5 of the extra 15 photos would be uploaded - the browser does upload all, but won't actually be uploaded to WordPress (or the target destination). For front-end solution, you can use JavaScript to remove the extra photos - I can help, if you can show the form HTML.
    – Sally CJ
    Commented Dec 13, 2018 at 17:18
  • Ok thanks Im going to test it out. As far as the form, its a lot of code, I have a lot going on with it, is there something specific I can show you that may help?
    – 730wavy
    Commented Dec 14, 2018 at 14:33
  • Try this demo.
    – Sally CJ
    Commented Dec 15, 2018 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.