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.
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
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. Security
Your server side validation needs to be good –
remember the data submitted is going straight into
your database.
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. 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. 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. 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
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