Handling User Generated Content in WordPress

Post on 19-Jun-2015

443 views 0 download

Tags:

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.

Transcript of Handling User Generated Content in WordPress

Coolfields Consulting www.coolfields.co.uk@coolfields

Handling user-generated content in WordPress

Graham ArmfieldWeb Accessibility ConsultantWordPress Developer

graham.armfield@coolfields.co.uk@coolfields

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.

What you want to avoidAn example – a gig listing

site

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

4

What you want to avoid

Adding all the gigs yourself

5

Advantages

More time for the important things

6

Advantages

People could be more engaged with your site

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

People will try to spam you

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.

Security

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

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

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

}

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.

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;

}

Approving gigs

Deciding which ones to publish

16

Approval page

• Create in admin area or private page on front end

• Display each gig in turn

• Three buttons • Approve• Edit Required• Delete

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

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

Thanks for listening

graham.armfield@coolfields.co.uk@coolfields