Allow Users To Submit Images To Your WordPress Site

Allow Users To Submit Images To Your WordPress Site

Tutorial Details
  • Program: WordPress
  • Version: 3.0+
  • Difficulty: Beginner to Intermediate
  • Estimated Completion Time: 30 minutes

In this tutorial, you’ll learn how to create a plugin that allows users to submit images and upload them to the WordPress media library. You’ll also learn how to correctly delete images from the WordPress media library as well as do some basic validation on uploaded images.


Previously…

This tutorial is by request from some users who found my quotes plugin tutorial interesting but were especially keen to find out how the same technique could be used to upload images from the frontend. So here’s a reiteration of that tutorial that does just that. For detailed info about plugin setup, shortcodes and nonces, see the previous tutorial.

The plugin will:

  • display an image upload form using a shortcode
  • accept only images of a certain type and maximum size
  • add a custom post type for user images
  • add images to the WordPress media library with a proper attachment caption
  • display unpublished images
  • allow users to delete their unpublished images

We will use the built-in WordPress post thumbnail (aka Featured Image) meta field to hold the image for each post. This also makes it easier to display and work with our image as we can use the post_thumbnail functions.

Here’s what we’re aiming for:

All code is available in the plugin source at the top of this tutorial.


Step 1 Set-Up the Plugin

Create a plugin file named submit_user_images.php in the wp-content/plugins/submit-user-images directory.

Refer to the plugin source for plugin header info.


Step 2 Plugin Initialization Function

We’re going to create a custom post type named user_images to hold our user images and a custom taxonomy named user_image_category. This will enable cleaner administration of the images than simply assigning them to normal posts and categories.

The Init Hook and Function

We’ll use the following initialization code to create our custom post type and custom taxonomy:

add_action('init', 'sui_plugin_init');

function sui_plugin_init(){

  $image_type_labels = array(
    'name' => _x('User images', 'post type general name'),
    'singular_name' => _x('User Image', 'post type singular name'),
    'add_new' => _x('Add New User Image', 'image'),
    'add_new_item' => __('Add New User Image'),
    'edit_item' => __('Edit User Image'),
    'new_item' => __('Add New User Image'),
    'all_items' => __('View User Images'),
    'view_item' => __('View User Image'),
    'search_items' => __('Search User Images'),
    'not_found' =>  __('No User Images found'),
    'not_found_in_trash' => __('No User Images found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'User Images'
  );
  
  $image_type_args = array(
    'labels' => $image_type_labels,
    'public' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'map_meta_cap' => true,
    'menu_position' => null,
    'supports' => array('title', 'editor', 'author', 'thumbnail')
  ); 
  
  register_post_type('user_images', $image_type_args);

  $image_category_labels = array(
    'name' => _x( 'User Image Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'User Image', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search User Image Categories' ),
    'all_items' => __( 'All User Image Categories' ),
    'parent_item' => __( 'Parent User Image Category' ),
    'parent_item_colon' => __( 'Parent User Image Category:' ),
    'edit_item' => __( 'Edit User Image Category' ), 
    'update_item' => __( 'Update User Image Category' ),
    'add_new_item' => __( 'Add New User Image Category' ),
    'new_item_name' => __( 'New User Image Name' ),
    'menu_name' => __( 'User Image Categories' ),
  ); 	

  $image_category_args = array(
    'hierarchical' => true,
    'labels' => $image_category_labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'user_image_category' ),
  );
  
  register_taxonomy('sui_image_category', array('user_images'), $image_category_args);
  
  $default_image_cats = array('humor', 'landscapes', 'sport', 'people');
  
  foreach($default_image_cats as $cat){
  
    if(!term_exists($cat, 'sui_image_category')) wp_insert_term($cat, 'sui_image_category');
    
  }
    
}

What this code does:

We will now have a User Images menu in our admin dashboard and a way to administer user images and their categories.


Step 3 Set Up Some Defaults

We’ll need to do some basic validation so let’s define two constants for later use:

define('MAX_UPLOAD_SIZE', 200000);
define('TYPE_WHITELIST', serialize(array(
  'image/jpeg',
  'image/png',
  'image/gif'
  )));

Step 4 Define A Shortcode

We’ll define a shortcode that will allow us to display (and process) the user images submission form in a post or page:

add_shortcode('sui_form', 'sui_form_shortcode');

Security

Because our plugin accepts data from the user, we implement the following security mechanisms:

  • only logged-in users have access to the image submission form
  • we use nonces to verify that the forms were generated by our plugin
  • images are submitted using wp_insert_post which sanitizes the data before saving it to the database
  • users can only view their own images, and nonces prevent them from deleting any other user’s image posts

Step 5 The Main Function

This is the function called by our shortcode. It displays and processes the image submission form and the image listing/deletion form. We’ll take it in bite-sized pieces and in Step 6 we’ll look at the helper functions.

function sui_form_shortcode(){

  if(!is_user_logged_in()){
  
    return '<p>You need to be logged in to submit an image.</p>';    

  }

  global $current_user;
  • check to see if the user is logged in
  • grab the WordPress $current_user variable which we’ll need to get our user ID
if(isset( $_POST['sui_upload_image_form_submitted'] ) && wp_verify_nonce($_POST['sui_upload_image_form_submitted'], 'sui_upload_image_form') ){  

  $result = sui_parse_file_errors($_FILES['sui_image_file'], $_POST['sui_image_caption']);
  
  if($result['error']){
  
    echo '<p>ERROR: ' . $result['error'] . '</p>';
  
  }else{

    $user_image_data = array(
      'post_title' => $result['caption'],
      'post_status' => 'pending',
      'post_author' => $current_user->ID,
      'post_type' => 'user_images'     
    );
    
    if($post_id = wp_insert_post($user_image_data)){
    
      sui_process_image('sui_image_file', $post_id, $result['caption']);
    
      wp_set_object_terms($post_id, (int)$_POST['sui_image_category'], 'sui_image_category');
    
    }
  }
}  
  • if the image form has been submitted, there’ll be a sui_upload_image_form_submitted field which was generated by our wp_nonce_field function. We can then verify the nonce and proceed to process the submitted image
  • do some validation by passing the file input (where the uploaded image data is stored) and caption input data to a validation function, sui_parse_file_errors, and display any returned errors
  • construct an array setting the post status to pending (the admin will now have to approve it for publication), setting the post type to user_images (our custom post type), and setting the author of the image post to the currently logged-in user
  • if the image post was successfully inserted, save the image in the WordPress media library (sui_process_image) and finally set the category for the image post and display a success message
if (isset( $_POST['sui_form_delete_submitted'] ) && wp_verify_nonce($_POST['sui_form_delete_submitted'], 'sui_form_delete')){

  if(isset($_POST['sui_image_delete_id'])){
  
    if($user_images_deleted = sui_delete_user_images($_POST['sui_image_delete_id'])){        
    
      echo '<p>' . $user_images_deleted . ' images(s) deleted!</p>';
      
    }
  }
}
  • if the image delete form has been submitted, there’ll be a sui_form_delete_submitted field which was generated by our wp_nonce_field function. We can then verify the nonce and proceed to process the array of images checked for deletion
  • we check that we actually have some images checked for deletion by testing $_POST['sui_image_delete_id']. If so, we hand them off to the sui_delete_user_images function (see Step 6)
  • if images were deleted, we display a success message
echo sui_get_upload_image_form($sui_image_caption = $_POST['sui_image_caption'], $sui_image_category = $_POST['sui_image_category']);

if($user_images_table = sui_get_user_images_table($current_user->ID)){

  echo $user_images_table;
  
}
  • we output the image upload form
  • finally, we output the images listing/deletion form by passing the user ID to the sui_get_user_images_table function (see Step 6)

Step 6 Helper Functions

Here we’ll look at the functions that generate the forms, add the images to the media library and the function that deletes the selected images.

function sui_get_upload_image_form($sui_image_caption = '', $sui_image_category = 0){

  $out = '';
  $out .= '<form id="sui_upload_image_form" method="post" action="" enctype="multipart/form-data">';

  $out .= wp_nonce_field('sui_upload_image_form', 'sui_upload_image_form_submitted');
  
  $out .= '<label for="sui_image_caption">Image Caption - Letters, Numbers and Spaces</label><br/>';
  $out .= '<input type="text" id="sui_image_caption" name="sui_image_caption" value="' . $sui_image_caption . '"/><br/>';
  $out .= '<label for="sui_image_category">Image Category</label><br/>';  
  $out .= sui_get_image_categories_dropdown('sui_image_category', $sui_image_category) . '<br/>';
  $out .= '<label for="sui_image_file">Select Your Image - ' . MAX_UPLOAD_SIZE . ' bytes maximum</label><br/>';  
  $out .= '<input type="file" size="60" name="sui_image_file" id="sui_image_file"><br/>';
    
  $out .= '<input type="submit" id="sui_submit" name="sui_submit" value="Upload Image">';

  $out .= '</form>';

  return $out;
  
}
  • the function accepts 2 optional arguments for repopulating the form fields. This is a convenience for the user.
  • a nonce field is output which we check when the form is submitted
  • we output a dropdown for the image categories by calling sui_get_image_categories_dropdown (see next function)
function sui_get_image_categories_dropdown($taxonomy, $selected){

  return wp_dropdown_categories(array('taxonomy' => $taxonomy, 'name' => 'sui_image_category', 'selected' => $selected, 'hide_empty' => 0, 'echo' => 0));

}
  • the function accepts 2 arguments including the element ID of the currently selected category
  • we use the WordPress wp_dropdown_categories function to create a dropdown that lists the user image categories from the user_image_category taxonomy (our custom taxonomy)
function sui_get_user_images_table($user_id){

  $args = array(
    'author' => $user_id,
    'post_type' => 'user_images',
    'post_status' => 'pending'    
  );
  
  $user_images = new WP_Query($args);

  if(!$user_images->post_count) return 0;
  
  $out = '';
  $out .= '<p>Your unpublished images - Click to see full size</p>';
  
  $out .= '<form method="post" action="">';
  
  $out .= wp_nonce_field('sui_form_delete', 'sui_form_delete_submitted');  
  
  $out .= '<table id="user_images">';
  $out .= '<thead><th>Image</th><th>Caption</th><th>Category</th><th>Delete</th></thead>';
    
  foreach($user_images->posts as $user_image){
  
    $user_image_cats = get_the_terms($user_image->ID, 'sui_image_category');
    
    foreach($user_image_cats as $cat){
    
      $user_image_cat = $cat->name;
    
    }
    
    $post_thumbnail_id = get_post_thumbnail_id($user_image->ID);   

    $out .= wp_nonce_field('sui_image_delete_' . $user_image->ID, 'sui_image_delete_id_' . $user_image->ID, false); 
       
    $out .= '<tr>';
    $out .= '<td>' . wp_get_attachment_link($post_thumbnail_id, 'thumbnail') . '</td>';    
    $out .= '<td>' . $user_image->post_title . '</td>';
    $out .= '<td>' . $user_image_cat . '</td>';    
    $out .= '<td><input type="checkbox" name="sui_image_delete_id[]" value="' . $user_image->ID . '" /></td>';          
    $out .= '</tr>';
    
  }

  $out .= '</table>';
    
  $out .= '<input type="submit" name="sui_delete" value="Delete Selected Images" />';
  $out .= '</form>';  
  
  return $out;

}
  • accept the user ID because we need to get a listing of user images for the current user only
  • create $args to specify our user, the post type of user_images and user images that are pending (not yet published by the admin)
  • execute a custom query using WP_Query
  • return false if our query returns no user images
  • start a form and generate a nonce for the form
  • loop through the images posts making sure we also grab the category of the image post
  • generate a nonce for the image delete checkbox, assigning a unique name for the nonce by concatenating the user image post ID
  • output a table row containing the image post info as well as a delete checkbox

Why add a nonce for each image post?

Forms can be manipulated in the browser to post back unexpected data. In our case, each delete checkbox is assigned the value of a post. But what if a malicious user altered that value and caused our delete function to remove a post that was not actually listed?

One way to avoid this, is to use nonces for each row of post data, ensuring that the nonces are uniquely named with the post value to be deleted. We then verify the nonce upon form submission to make sure it’s a genuine return value.

function sui_delete_user_images($images_to_delete){

  $images_deleted = 0;

  foreach($images_to_delete as $user_image){

    if (isset($_POST['sui_image_delete_id_' . $user_image]) && wp_verify_nonce($_POST['sui_image_delete_id_' . $user_image], 'sui_image_delete_' . $user_image)){
    
      if($post_thumbnail_id = get_post_thumbnail_id($user_image)){

        wp_delete_attachment($post_thumbnail_id);      

      }  

      wp_trash_post($user_image);
      
      $images_deleted ++;

    }
  }

  return $images_deleted;

}
  • the function accepts an array of image post IDs to delete
  • each image post ID is checked to see if a nonce was generated for it
  • if the nonce verifies, we delete the image attachment that exists in the media library by passing the id of the image post thumbnail to the WordPress function wp_delete_attachment
  • we also trash the image post using the WordPress function wp_trash_post

But doesn’t the thumbnail attachment get deleted when the post is trashed?

No and that’s because WordPress stores attachments as regular posts in the posts database table. Have a look yourself: all attachments are stored in the posts table with a post_type of attachment. Simply deleting a post of type user_images doesn’t delete its thumbnail attachment. It remains in the media library for future use unless we specifically delete it with wp_delete_attachment. For our purposes, I thought it was best to remove the attachment when the user’s post was deleted.


Step 7 The Image Handling Functions

Let’s remind ourselves of what the output of an html file input looks like when it posts an image to your script:

Array
(
    [name] => ref_blind.jpg
    [type] => image/jpeg
    [tmp_name] => /tmp/php79xI4e
    [error] => 0
    [size] => 106290
)

We pass that array to the sui_process_image function along with the id of the saved user image post and the santized image caption.

function sui_process_image($file, $post_id, $caption){
 
  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  require_once(ABSPATH . "wp-admin" . '/includes/media.php');
 
  $attachment_id = media_handle_upload($file, $post_id);
 
  update_post_meta($post_id, '_thumbnail_id', $attachment_id);

  $attachment_data = array(
  	'ID' => $attachment_id,
    'post_excerpt' => $caption
  );
  
  wp_update_post($attachment_data);

  return $attachment_id;

}
  • we need to include the WordPress admin scripts that handle image uploads behind the scenes
  • we call the media_handle_upload function (which is part of media.php), passing it the uploaded file array and the post id
  • now we have an attachment id which we can use with update_post_meta to assign the attachment to the post as its thumbnail. Note: “_thumbnail_id” refers to the internal thumbnail (Featured Image) meta field. Internal WordPress fields begin with an underscore.
  • we next use the attachment id to update the caption of the attachment using the wp_update_post function

Because attachments are just regular posts, if we update the post_excerpt field for the attachment, we are actually updating the attachment’s caption field as seen in the media library edit screen.

The validation function

We also validate the file array and the user-provided image caption with the sui_parse_file_errors function.

function sui_parse_file_errors($file = '', $image_caption){

  $result = array();
  $result['error'] = 0;
  
  if($file['error']){
  
    $result['error'] = "No file uploaded or there was an upload error!";
    
    return $result;
  
  }

  $image_caption = trim(preg_replace('/[^a-zA-Z0-9\s]+/', ' ', $image_caption));
  
  if($image_caption == ''){

    $result['error'] = "Your caption may only contain letters, numbers and spaces!";
    
    return $result;
  
  }
  
  $result['caption'] = $image_caption;  

  $image_data = getimagesize($file['tmp_name']);
  
  if(!in_array($image_data['mime'], unserialize(TYPE_WHITELIST))){
  
    $result['error'] = 'Your image must be a jpeg, png or gif!';
    
  }elseif(($file['size'] > MAX_UPLOAD_SIZE)){
  
    $result['error'] = 'Your image was ' . $file['size'] . ' bytes! It must not exceed ' . MAX_UPLOAD_SIZE . ' bytes.';
    
  }
    
  return $result;

}
  • check the error element of the files array for an html upload error, if found return a result array with error
  • run some regex on the image caption to remove everything but alphanumeric data and spaces, replacing with spaces for readability
  • if we end up with an empty caption after sanitizing it, we return an error
  • check the internal image type (don’t trust the file extension) using the PHP getimagesize function against the TYPE_WHITELIST constant
  • check the image size against the MAX_UPLOAD_SIZE constant

Step 8 Some Styling

Just drop this style info into the style.css file in your theme folder:

#sui_upload_image_form #sui_image_caption{
  width:500px;
}
#user_images{
  font-size:12px;
}
#user_images th{
  text-align:left;
}
#user_images td{
  vertical-align:middle;
}
#user_images td input{
  margin:0px;
}    

Step 9 Try It Out

Activate the plugin, pop the shortcode onto a page, log into your site, and test it out. When you upload an image you’ll see a new post appear under the User Images admin menu. It will be pending publication. You’ll also see a new image listed in your media library, attached to your new post and with the caption as provided.

The full plugin code source and a demo site link is listed at the top of this tutorial.

The source folder also contains a WordPress page template with a custom loop that displays published images for all users.


Final Thoughts

You may wish to place more stringent validation on your image uploads. Remember, you’re accepting data from users who may accidentally or maliciously upload inappropriate files. Checking the file type and size is a good start.

Also, we created the attachment caption by updating the attachment’s post_excerpt field. You can also set an attachment description by using the attachment’s post_content field.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • jerichvc

    “Allow Users To Submit Images Your WordPress Site”
    check title?

    • Brandon Jones

      Fixed :)

  • http://iamzachreed.com/ Zach Reed

    Very good tutorial and very glad you decided to do this follow up article! I’m glad you listened to your readers in the comment! Great work and great tut! :)

  • http://tobstudios.com Cory

    For the less technical people: You can use the NextGen Public Uploader plugin, combined with the NextGen gallery plugin.

    Good post.

  • http://titan-creative.net S3bY

    This is one of the best tutorials since the blog launched! I tried making a function like this for quite a while without success!

    • http://wp.rosselliot.co.nz Ross Elliot
      Author

      Thanks! Glad you found it helpful.

    • http://whysodumb.com Abhimanyu

      Totally!

  • http://www.agnelwaghela.bammz.net/ Agnel Waghela

    Hii, I’m very happy to tell ya that my search ends here. This is the plugin I was searching for a while. Thanks Ross for this wonderful Plugin. I have an idea about this plugin that is if like wordpress 3.3 drag and drop upload method is used for uploading the images…? Well this is just a suggestion for plugin development, what’d you say man?

  • http://inventanew.com john

    I get this error when I view the images that were uploaded?

    Invalid argument supplied for foreach()

    • http://wp.rosselliot.co.nz Ross Elliot
      Author

      John, do you mean on the image submission page or the page for viewing all user images (the included template)?

      • http://inventanew.com john

        it was the admin page where you view all user images, there are three errors that pop up…

        Warning: Invalid argument supplied for foreach() in /home/…/…/wp-admin/includes/class-wp-list-table.php on line 661

        Warning: Invalid argument supplied for foreach() in /home/…/…/wp-admin/includes/class-wp-list-table.php on line 661

        Warning: Invalid argument supplied for foreach() in /home/…/…/wp-admin/includes/class-wp-posts-list-table.php on line 487

        it is installed on a multisite environment with the most current versions of wordpress and buddypress. I activated it from the network admin.

        Thanks for responding, very nice tutorial.

        • http://unrelatedmedia.ca Neil Davidson

          Actually, I found this issue as well several months back when I created a nearly identical tutorial on my own website – just not taking the plugin approach. It was part of a custom profile series.

          The first tutorial simply uploads a single attachment to wordpress using the built in media handler.

          The second tutorial uses the same approach but takes the php FILES array and breaks it up into a WordPress usable format.

          Now, at first I was confused and was blaming my script and troubleshooting with try{} catch{} blocks all throughout my php to no avail. What I didn’t think about is that I recently updated wordpress (3.0 at the time I believe) and was able to clear it all up with a plugin called Hot Fix which I now also include on all my WP installs just to be safe.

          Now, if that doesn’t work I would back-up your files and completely re-upload a recent version of wordpress MU and then restore your data.

  • Pingback: Allow To Submit Images To Your WordPress Site To The Users | Web Help 101

  • http://nelermoda.com Erkan

    I want to upload image but with my plugin, like in media.php. I couldn’t your method for my plugin :( Can you make for me? Please. You can download my plugin http://www.nelermoda.com/Eerkan.rar
    Thanks.

  • AdamH

    Can you do the same thing with video and audio?

    • http://wp.rosselliot.co.nz Ross Elliot
      Author

      You can certainly upload audio and video as an attachment, but you won’t be able to attach them to a post as the featured image (the thumbnail) obviously.

      But the same uploading procedure applies, in general.

    • http://unrelatedmedia.ca Neil Davidson

      Actually, you are using a form and processing in PHP. WordPress just has it’s own method of manipulating the data being sent. At the end of my multiple file uploading with WordPress I have a function called Upload_Mime_Type() which has a complete array of file types you can check for.

      Just in case the below doesn’t paste right take a look at my page and yu’ll see it. To use it simply feed it the function which returns a boolean true/false. If there is a file type in the array you don’t want to be accepted as an uploaded type simply comment it out of the array. If nothing else it gives you a good idea of acceptable file types!

      function Uploaded_Mime_Type() {
      //edit this array to limit accepted file types
      $mime_types = array(
      ‘txt’ => ‘text/plain’,
      ‘htm’ => ‘text/html’,
      ‘html’ => ‘text/html’,
      ‘php’ => ‘text/html’,
      ‘css’ => ‘text/css’,
      ‘js’ => ‘application/javascript’,
      ‘json’ => ‘application/json’,
      ‘xml’ => ‘application/xml’,
      ‘swf’ => ‘application/x-shockwave-flash’,
      ‘flv’ => ‘video/x-flv’,

      // images
      ‘png’ => ‘image/png’,
      ‘jpe’ => ‘image/jpeg’,
      ‘jpeg’ => ‘image/jpeg’,
      ‘jpg’ => ‘image/jpeg’,
      ‘gif’ => ‘image/gif’,
      ‘bmp’ => ‘image/bmp’,
      ‘ico’ => ‘image/vnd.microsoft.icon’,
      ‘tiff’ => ‘image/tiff’,
      ‘tif’ => ‘image/tiff’,
      ‘svg’ => ‘image/svg+xml’,
      ‘svgz’ => ‘image/svg+xml’,

      // archives
      ‘zip’ => ‘application/zip’,
      ‘rar’ => ‘application/x-rar-compressed’,
      ‘exe’ => ‘application/x-msdownload’,
      ‘msi’ => ‘application/x-msdownload’,
      ‘cab’ => ‘application/vnd.ms-cab-compressed’,

      // audio/video
      ‘mp3′ => ‘audio/mpeg’,
      ‘qt’ => ‘video/quicktime’,
      ‘mov’ => ‘video/quicktime’,

      // adobe
      ‘pdf’ => ‘application/pdf’,
      ‘psd’ => ‘image/vnd.adobe.photoshop’,
      ‘ai’ => ‘application/postscript’,
      ‘eps’ => ‘application/postscript’,
      ‘ps’ => ‘application/postscript’,

      // ms office
      ‘doc’ => ‘application/msword’,
      ‘rtf’ => ‘application/rtf’,
      ‘xls’ => ‘application/vnd.ms-excel’,
      ‘ppt’ => ‘application/vnd.ms-powerpoint’,

      // open office
      ‘odt’ => ‘application/vnd.oasis.opendocument.text’,
      ‘ods’ => ‘application/vnd.oasis.opendocument.spreadsheet’,
      );
      if(in_array($_FILES['async-upload']['type'],$mime_types)){
      return true;
      }
      else{return false;}
      }

  • http://www.funkgrapicz.net david

    source code was not working

  • http://inventanew.com john

    How can I tweak the template you have in the download so that it only shows images from certain categories? That would be the bees knees. I am using it with all my students in class.

    • http://inventanew.com john

      Please, can anyone help me with this, I really want to figure out how to show only certain categories. I will pay money.

  • PraveshSingh

    This is best one article so far I have read online. I would like to appreciate you for making it very simple and easy. I have found another nice post related to this post over the internet which also explained very well. For more details you may check it by visiting this url.

    http://mindstick.com/Articles/06303fa8-d3d6-4a00-8a9b-4837189237ab/?Validating%20a%20Website%20using%20WordPress

  • Pingback: 30 WordPress Plugin Tutorial | WordpressRadar.com - The Best Collection of Wordpress Tutorials on the Web

  • Chris

    This tutorial looks good.

    Is there a way you can restrict the Dimensions of the Image to be uploaded? So only an image of say 500 x 500 can be uploaded?

    Cheers
    Chris

    • http://www.edibleshowcase.com Anthony

      You can set the image dimensions that can be uploaded in the WordPress Settings->Media section. I think this is not really restriction, but more of an edit after the image is uploaded, but it works.

      This was a good and very helpful post. I actually made some changes to have the images post side by side in row/column format instead of one row at a time. I am very new to PHP so this took me a while to figure out, but its easy to implement. I am not sure if i did it the best way, but it works and I can control how many posts displays on the page. Check out my site at http://www.edibleshowcase.com, its still in the early development stages as I learn to use PHP with WordPress . If anyone wants to know how i did it, I will respond.

      My current struggle is figuring out how to make the page show posts by category.

  • http://luisdiego.com Luis Diego

    Really thanks!

    • http://luisdiego.com Luis Diego

      When i set WP_DEBUG = true, it throwed an error inl line 73, i just used the isset function to $_POST['sui_image_caption'], and the error disappered..

      $sui_image_caption=”";
      if(isset($_POST['sui_image_caption'])) { $sui_image_caption = $_POST['sui_image_caption'];}
      echo sui_get_upload_image_form($sui_image_caption);

  • http://www.gooretail.com Johan

    Tnx for great tutorial. I just wonder howto approve uploaded images? I just can´t figure it out… Anyone?

    • http://metalwebsitedesign.co.uk/ Lee Day

      all you’d need to do is set the post status to pending,
      'post_status' => 'pending'

  • http://hotels-advisor.com/ Trip Advisor

    Hi
    Is there any plugin like this:

    User can upload file and image in a post
    Admin approve the post
    after it get approved if someone buy that user file the user will get money and admin will get a percentage from that.
    Please help me out
    i Google it and didn’t find anything close.
    There are other CMS for that but i want it in my wordpress :)
    Thank you

  • tre

    Just got this error using the source code:

    Fatal error: Call to undefined function get_post_thumbnail_id() in C:\xampp\htdocs\ghosts\wp-content\plugins\SUI\submit_user_images.php on line 142.

    Odd that a function built in to wordpress would be ‘undefined’.

    Anyone else have this problem?

  • http://www.tikiwebgroup.com tiki god

    I’m trying to understand why there’s a post being generated at all, and if there’s a post, why not insert the image thumbnail (with a link to the attachment) into the post contents automatically?

    and why, when I delete the post, the images are sticking around, isn’t that part of Part 6? or is that only for images that are deleted from the upload page?

  • Paco

    Hi, thanks for the good plugin !

    I’m a problem. When an user upload a file, the file is correctly upload. But if you browse to the site and you comeback in the browsing history to the upload page, a second execution of the form is realise and the image is upload again.

    How to solve this problem?

  • Manoj

    Hi, thanks for the tutorial. Its a great one. Thank you very much. I have a doubt

    The above code is repeated twice – one before the “form” element and another one below the “form” element.

    Why is that so?

  • Pingback: plugins | Pearltrees

  • Fu

    It so complicated explained! very bad!
    We dont have devoloped an microsoft software… maybe here are some who can used a little bit php in his live.
    anyway, why u dont describe where I should but this codes?

    maybe but this code in xxxx.php…

    and prefer this code in header… ok but HOW?!

  • Pingback: media | Pearltrees

  • Pingback: WordPress plugins for user-submitted & user-generated content | linuxin.ro

  • Pingback: WordPress plugins for user-submitted & user-generated content |

  • Pingback: WordPress plugins for user-submitted & user-generated content | Blue Sparrow Media

  • Pingback: Robert Rosić » WordPress plugins for user-submitted & user-generated content

  • Lee Ming Zhen

    Hi,

    How can i view all users the uploaded picture in a page. Urgent. Thanks you

    Regards,
    Ivan Lee

  • Pingback: WordPress plugins for user-submitted & user-generated content | Top website Designing Company in India

  • kairess

    Hi. very good plugin for every one!
    I want to attach multiple images at once… but I dont have any idea.
    I read all comments and Neil Davidson’s reply also. still no idea.
    someone help me?

  • Ilton Alberto

    Thank you, this plugin help me a lot, but I need one last thing:

    How can I do to limit the user upload to 1 upload per member? I’m making a image contest site and I really need this.

    ;)

  • Pingback: Složitější galerie « Fórum podpory WordPressu

  • traubisoda

    how do i put an upload button to my site? :/

  • Rita

    Trying to figure out how to change the default category names of ‘humor’, ‘landscapes’, ‘sports’, and ‘people’ to my own categories. Anyone who knows how to change it?

    Appreciate your help.

    • http://wp.envato.com/ Japh Thomson
      Staff

      Hi Rita, check line 52 of the code snippet in Step 2. You can see the default categories are defined there. Hope that helps :)

  • http://www.facebook.com/marilyn.wong.5667 Marilyn Wong

    Great tutorial, thank you. Is there anyway to send the uploaded images to the “post” section in wordpress as featured image rather than in the created “user image”?

  • http://www.facebook.com/gastonbesada Gaston Besada

    Some plugins for this amazing plugin!
    http://www.html5rocks.com/en/tutorials/file/dndfiles/

  • slightlyeskew

    Hey I’ve uploaded the plugin but when I navigate to “website.com”/user-images/submit-user-images/ the page is not found! Any ideas on getting this to work? I’ve activated the plugin and tried several variations on the URL to no avail. Any help would be much appreciated.

    • Joey Reed

      Sounds like you have a permalink issue, which is often the case when you register a new custom post type. Here’s how to fix:

      1) Go to Settings->Permalinks.

      2) Reset your permalinks to the ‘Default’ setting, then save changes.

      3) Change back to your custom permalink settings, then save changes.

      That should solve the problem!

  • http://www.facebook.com/RafaelHasbun Rafael Hasbun

    What is the shortcode?

    • http://www.facebook.com/mittul.chauhan Mittul Chauhan

      [sui_form]

      use this shortcode in yor posts or pages .. try this

  • Bolorino

    I was having troubles with the form getting displayed twice.
    The problem are the echo statements in the shortcode function (sui_form_shortcode).
    It was solved replacing the echoes with $output = ‘text’ and returning $output as a result.

  • http://www.facebook.com/RafaelHasbun Rafael Hasbun

    How can I display the user submitted images on a different page?

  • http://www.facebook.com/somdefabrica Cesar Oliveira

    Hello from Brazil :) Thanks, working 100%

    But I wish add a Permalink to ‘ . $user_image->post_title . ‘

    my wordpress and buddypress http://www.cidadesonho.com/anuncios

  • http://www.facebook.com/somdefabrica Cesar Oliveira

    Hey :( where’s my comment ? Sorry for anything …

    I just wish add a permalink on ‘ . $user_image->post_title . ‘

    user_images_template.php

  • sorin

    nice tutorial. I have a wp theme where users may upload up to 4 images in a form together with other data fields. images are uploaded in db as attachments, each one has an ID. question is how can I retrieve all images from db, show in an user edit form, where user can replace or delete them?

    • http://metalwebsitedesign.co.uk/ Lee Day

      Maybe add those IDs to the users account with add_user_meta, then u can get these IDs with get_user_meta and query wordpress for them useing these IDs.

      I’m just going to recycle some of the code above, namely:

      $args = array(
      'author' => $user_id,
      'post_type' => 'user_images',
      'post_status' => 'pending'
      );

      $user_images = new WP_Query($args);
      if(!$user_images->post_count) return 0;

      foreach($user_images->posts as $user_image){
      $post_thumbnail_id = get_post_thumbnail_id($user_image->ID);
      }

  • Cesar Oliveira

    How can I add post-formats ? Thanks ! Amazing!

  • http://metalwebsitedesign.co.uk/ Lee Day

    Is there anyway to associate the custom post type with a user, and then be able to retrieve these for a given user lateron.
    For instance, user uploads image from a custom page type, ” http://www.website.com/upload “, then on their profile page or in their account settings page, get their upload files and display them with a loop?
    I was thinking of adding a custom field to the image post type with the users ID but i fear using WP_Query lateron could get slow when there’s a lot of images and the site is starting to fill up, obviously it would be better to store the image post ID’s in the users account somehow instead of doing it backwards

  • Roberto

    Great Tutorial!!! There’s any way to upload photo from smartphone by users?