'testimonial',
'numberposts' => 1
);
if( is_numeric( $atts['id'] ) )
$args['include'] = $atts['id'];
elseif( $atts['id'] == 'random' )
$args['orderby'] = 'rand';
if( $testimonials = get_posts( $args ) ) {
ob_start();
foreach( $testimonials as $testimonial ) {
$testimonial = new WP_Testimonial( $testimonial->ID );
$testimonial->render();
}
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
add_shortcode( 'testimonial', 'shortcode_testimonial' );
// Handler for "testimonials" shortcode.
// This shortcode is used to output multiple testimonials from a testimonial_category term
function shortcode_testimonials ( $atts ) {
$args = array(
'posts_per_page' => isset( $atts['per_page'] ) ? $atts['per_page'] : 2,
'paged' => get_query_var( 'paged' ),
'post_type' => 'testimonial',
);
$output = '';
if( isset( $atts['category'] ) ) {
$category = get_term_by( 'id', $atts['category'], 'testimonial_category' );
$args['testimonial_category'] = $category->slug;
}
if( query_posts( $args ) ) {
if( have_posts() ) {
ob_start();
echo '
';
while( have_posts( ) ) {
the_post();
$testimonial = new WP_Testimonial( get_the_ID() );
$testimonial->render();
}
if( function_exists( 'wp_paginate' ) ) {
wp_paginate();
}
else {
global $wp_query;
$big = 9999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
echo '
';
$output = ob_get_contents();
ob_end_clean();
}
wp_reset_query();
return $output;
}
}
add_shortcode( 'testimonials', 'shortcode_testimonials' );
// Handler for "testimonial-submission-form" shortcode
// This shortcode outputs a form which visitors can use to submit a testimonial
function shortcode_testimonial_submission ( $atts ) {
ob_start();
if( isset( $_POST['testimonial-postback'] ) && wp_verify_nonce( $_POST['testimonial_nonce'], 'add-testimonial' ) ):
// Require WordPress core functions we require for file upload
if( !function_exists( 'media_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
// Build post array object
$post = array(
'ID' => NULL,
'post_content' => apply_filters( 'the_content', esc_textarea( $_POST['testimonial_description'] ) ),
'post_name' => '',
'post_type' => 'testimonial',
'post_status' => 'draft',
'post_title' => $_POST['testimonial_title']
);
// Ensure CAPTCHA passed
require_once( trailingslashit( dirname( __FILE__ ) ) . 'recaptchalib.php' );
$captcha = recaptcha_check_answer(
'6Letc-kSAAAAANmcKKUmmybcly0ma7LXXc5Llcmm',
$_SERVER['REMOTE_ADDR'],
$_POST['recaptcha_challenge_field'],
$_POST['recaptcha_response_field']
)->is_valid;
// Insert new testimonial, if successful, update meta data
if( ( $post_id = wp_insert_post( $post, false ) ) && $captcha ) {
update_post_meta( $post_id, 'testimonial_client_name', sanitize_text_field( $_POST['testimonial_client_name'] ) );
update_post_meta( $post_id, 'testimonial_client_company_name', sanitize_text_field( $_POST['testimonial_client_company_name'] ) );
update_post_meta( $post_id, 'testimonial_client_email', sanitize_email( $_POST['testimonial_client_email'] ) );
update_post_meta( $post_id, 'testimonial_client_company_website', esc_url( $_POST['testimonial_client_company_website'] ) );
update_post_meta( $post_id, 'testimonial_client_permission', $_POST['permission'] == '' ? 'no' : sanitize_text_field( $_POST['permission'] ) );
// If thumbnail has been uploaded, assign as thumbnail
if( !empty( $_FILES['thumbnail']['tmp_name'] ) )
if( $attachment_id = media_handle_upload( 'thumbnail', $post_id ) )
update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
// If a category has been selected, update the object term
if( '' != $_POST['testimonial_category_group'] )
wp_set_object_terms( $post_id, $_POST['testimonial_category_group'], 'testimonial_category' );
echo 'We successfully received your testimonial. If approved, it will appear on our website. Thank you!
';
}
else {
echo 'Sorry, but there was a problem with submitting your testimonial. Please ensure all required fields have been supplied and that you entered the CAPTCHA code correctly.
';
}
else:
?>