Handling User Generated Content in WordPress


Published on

Running a listing or job board site can be a great experience and generate ad revenue. But you don't really want to add all the listings in yourself - that can be a lot of work.

So why not get your site visitors to add it for you, but without having to give them all a WordPress login.

This presentation focuses on the key techniques you can use to accept user generated content via a form on your WordPress website. You can use these techniques in many different ways, eg: for gig listings, general events, classified ads, job boards, company profiles, etc.

Published in: Technology, Art & Photos
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Handling User Generated Content in WordPress

  1. 1. Coolfields Consulting www.coolfields.co.uk @coolfields Handling user-generated content in WordPress Graham Armfield Web Accessibility Consultant WordPress Developer graham.armfield@coolfields.co.uk @coolfields
  2. 2. What I’m going to cover A way to allow your site visitors to add content for your WordPress site – without giving them a logon. 2
  3. 3. What you want to avoidAn example – a gig listing site But could be used for - Job Board, For Sale Site, Etc
  4. 4. 4 What you want to avoid Adding all the gigs yourself
  5. 5. 5 Advantages More time for the important things
  6. 6. 6 Advantages People could be more engaged with your site
  7. 7. First steps • Create a new post type – my_gig • Create a form on a page template • For a gig, typical information might be: • Band name • Band information • Gig venue • Gig date • Gig time 7 It's also sensible to collect details on the person submitting the event – name, email, etc
  8. 8. People will try to spam you
  9. 9. Preventing spam 9 Use a simple logic puzzle or sum to fool the bots. Don’t use a CAPTCHA – they’re a usability and accessibility nightmare.
  10. 10. Security Your server side validation needs to be good – remember the data submitted is going straight into your database.
  11. 11. Storing the information After validation (of course) $postAdd = array(); $postAdd['post_title'] = $clean['bandname']; $postAdd['post_content'] = $clean['bandinfo']; $postAdd['post_type'] = 'my_gig'; $postAdd['post_status'] = 'draft'; $gigId = wp_insert_post($postAdd); 11
  12. 12. Storing the custom fields Check for success and store the other details if ($gigId > 0) { // Insert successful - write the custom fields update_post_meta($gigId, 'gig_submitter_name', $clean['submittername']); update_post_meta($gigId, 'gig_submitter_email', $clean['submitteremail']); update_post_meta($gigId, 'gig_date', $clean['startdate']); etc... } 12
  13. 13. Storing any images What about image uploads – a photo of the band as a featured image? if ($_FILES and (!empty($clean['eventimg']))) { foreach ($_FILES as $file => $array) { $newupload = insert_attachment($file, $gigId); } } $newupload gets the attachment id of the file that was just uploaded. Do whatever you want with that now. 13
  14. 14. Storing any images function insert_attachment($file_handler,$post_id) { //upload successful? if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) { __return_false(); } require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); $attach_id = media_handle_upload($file_handler, $post_id ); // Make this image the post thumbnail update_post_meta($post_id,'_thumbnail_id',$attach_id); // Return the ID of the attached image return $attach_id; } 14
  15. 15. Approving gigs Deciding which ones to publish
  16. 16. Approval page • Create in admin area or private page on front end • Display each gig in turn • Three buttons • Approve • Edit Required • Delete 16
  17. 17. Approval or edit required $postAdd = array(); $postAdd['ID'] = $gigId; if ( $approved ) { $postAdd['post_status'] = 'publish'; } if ( $editReqd ) { $postAdd['post_status'] = 'pending'; } wp_update_post($postAdd); 17
  18. 18. Delete it wp_delete_post( $gigId, true ); The second parameter determines whether the post is deleted outright (true) or is moved to trash (false). 18
  19. 19. Thanks for listening graham.armfield@coolfields.co.uk @coolfields