title: Limit image gallery selected images to 5 maximum in wordpress
body: <p>im using native wordpress image uploader in my website front end so the user can upload images to the website.</p>
  
  <p>what i want to do is limit the photos selection only to 5 nothing more </p>
  
  <p>like this </p>
  
  <p><img src="https://i.stack.imgur.com/Cp6jr.png" alt="enter image description here"></p>
  
  <p>when the user try to select more, i want to stop him.</p>
  
  <p>here is my code i got so far and it's working fine except it allow user to select unlimited images</p>
  
  <pre><code>       &lt;ul class="product_images"&gt;
              &lt;?php
              $gallery = explode( ',', $product_images );
  
              if ( $gallery ) {
                  foreach ($gallery as $image_id) {
                      if ( empty( $image_id ) ) {
                          continue;
                      }
  
                      $attachment_image = wp_get_attachment_image_src( $image_id, 'thumbnail' );
                      ?&gt;
                      &lt;li class="image" data-attachment_id="&lt;?php echo $image_id; ?&gt;"&gt;
                          &lt;img src="&lt;?php echo $attachment_image[0]; ?&gt;" alt=""&gt;
                      &lt;/li&gt;
                      &lt;?php
                  }
              }
              ?&gt;
          &lt;/ul&gt;
  
          &lt;input type="hidden" id="product_image_gallery" name="product_image_gallery" value="&lt;?php echo esc_attr( $product_images ); ?&gt;"&gt;
  </code></pre>