WordPress + jQuery + PHP + MySQL: Subtract upload limit upon total count

I built simple review media system for WordPress site. I am limiting number of upload through jQuery where button changes the state to disable when user reaches to the limit. Let’s say 4 images is the limit.

I have created custom table called wp_review_media and using hook inserting data of id, post_id, user_id, image_name, status

Read More

Database Image

Currently system doesn’t check if user has already posted images with the limited amount and each time he visits allows again to add images. Now here I want to restrict and count each time when user click on add more button that how many upload limits remains. First time users can use current jQuery limit as no record in database for that user. But once he submit the form than it should check how many images user has been uploaded for hat past.

I have done everything but stuck on this count system to check with the database. Here are all my code what I wrote.

Upload Form:

<form id="file-upload" action="" method="POST" enctype="multipart/form-data">
    <div id="image-uploader-box" class="group">           
        <div id="forms" class="add-photo-fields">                    
            <input type="button" id="add-photo-button" class="add-photo-button" value="Add Photo"/>
            <input type="submit" value="Upload" />
        </div>
    </div> 
</form>

WP Localize Script:

if(!is_admin()){   
    $photo_num = ($rm_options['max_image_count'] - 1); // Here you fetch the option data.    
    // Enqueue your script
    wp_enqueue_script( 'rm-javascript', RM_URL.'js/rm-scripts.js', array('jquery'), 1, true );   
    // Set the data you want to pass to your script here
    $data = array( 'photo_button_cnt' => $photo_num );    
    // Localize the script, the 'photo_button_cnt' value will now be accessible as a property in the global 'add_photo_button' object.
    wp_localize_script( 'rm-javascript', 'add_photo_button', $data );
}

jQuery Code:

jQuery(document).ready(function(){

    $cnt = 0;

    jQuery('#add-photo-button').click(function(){    

    if($cnt == add_photo_button.photo_button_cnt ){ 
        jQuery('#add-photo-button').prop('disabled', true);
        jQuery('#add-photo-button').addClass('disabled');
    }
    $cnt++;
        var current_count = jQuery('input[type="file"]').length;
        //var next_count = current_count + 1;

        jQuery('#file-upload').prepend('<p><input type="file" name="photo[]" /></p>');    

    });

});

Image count method:

public function user_image_count()
{
    global $wpdb, $post, $current_user;
    get_currentuserinfo();
    $image_count = $wpdb->get_var("SELECT COUNT(user_id) AS total_images FROM {$wpdb->prefix}review_media WHERE post_id=$post->ID AND user_id=$current_user->ID GROUP BY post_id");

    return $image_count;        
}

I hope this would be enough information for what help I am looking for. Please let me know if you guyz need any further information.

Trillion thanks 🙂

Related posts

Leave a Reply