19
Coolfields Consulting www.coolfields.co. uk @coolfields Handling user-generated content in WordPress Graham Armfield Web Accessibility Consultant WordPress Developer [email protected] @coolfields

Handling User Generated Content in WordPress

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: Handling User Generated Content in WordPress

Coolfields Consulting www.coolfields.co.uk@coolfields

Handling user-generated content in WordPress

Graham ArmfieldWeb Accessibility ConsultantWordPress Developer

[email protected]@coolfields

Page 2: Handling User Generated Content in WordPress

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.

Page 3: Handling User Generated Content in WordPress

What you want to avoidAn example – a gig listing

site

But could be used for - Job Board, For Sale Site, Etc

Page 4: Handling User Generated Content in WordPress

4

What you want to avoid

Adding all the gigs yourself

Page 5: Handling User Generated Content in WordPress

5

Advantages

More time for the important things

Page 6: Handling User Generated Content in WordPress

6

Advantages

People could be more engaged with your site

Page 7: Handling User Generated Content in WordPress

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

It's also sensible to collect details on the person submitting the event – name, email, etc

Page 8: Handling User Generated Content in WordPress

People will try to spam you

Page 9: Handling User Generated Content in WordPress

9

Preventing spam

Use a simple logic puzzle or sum to fool the bots.

Don’t use a CAPTCHA – they’re a usability and accessibility nightmare.

Page 10: Handling User Generated Content in WordPress

Security

Your server side validation needs to be good – remember the data submitted is going straight into your database.

Page 11: Handling User Generated Content in WordPress

11

Storing the informationAfter 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);

Page 12: Handling User Generated Content in WordPress

12

Storing the custom fieldsCheck for success and store the other detailsif ($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...

}

Page 13: Handling User Generated Content in WordPress

13

Storing any imagesWhat 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.

Page 14: Handling User Generated Content in WordPress

14

Storing any imagesfunction 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;

}

Page 15: Handling User Generated Content in WordPress

Approving gigs

Deciding which ones to publish

Page 16: Handling User Generated Content in WordPress

16

Approval page

• Create in admin area or private page on front end

• Display each gig in turn

• Three buttons • Approve• Edit Required• Delete

Page 17: Handling User Generated Content in WordPress

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);

Page 18: Handling User Generated Content in WordPress

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).

Page 19: Handling User Generated Content in WordPress

Thanks for listening

[email protected]@coolfields