Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Created July 12, 2012 05:25
Show Gist options
  • Save BronsonQuick/3096007 to your computer and use it in GitHub Desktop.
Save BronsonQuick/3096007 to your computer and use it in GitHub Desktop.
A Testimonials Custom Post Type in a PHP class
<?php
/*
Plugin Name: Pico Themes Testimonials
Plugin URI: http://wwww.picothemes.com/
Description: This plugin generates a custom post type for Testimonials
Author: Bronson Quick
Version: 1.0
Author URI: http://www.picothemes.com/
*/
class Pico_Testimonials {
public function __construct()
{
add_action( 'init', array( &$this, 'register_cpt_testimonial'));
add_action( 'add_meta_boxes', array ( &$this, 'pico_add_meta_boxes') );
add_action( 'save_post', array( &$this, 'pico_save_testimonial_meta' ) );
add_action( 'add_meta_boxes_testimonial', array( &$this, 'pico_testimonials_add_meta_boxes' ) );
add_filter( 'admin_post_thumbnail_html', array( &$this, 'pico_testimonials_post_thumbnail_html' ) );
add_filter( 'post_updated_messages', array( &$this, 'pico_testimonial_updated_messages') );
}
public function register_cpt_testimonial() {
$args = array(
'public' => true,
'query_var' => 'testimonial',
'rewrite' => array(
'slug' => 'testimonials',
'with_front' => false
),
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'revisions'
),
'labels' => array(
'name' => __( 'Testimonials', 'saas' ),
'singular_name' => __( 'Testimonial', 'saas' ),
'add_new' => __( 'Add New', 'saas' ),
'add_new_item' => __( 'Add New', 'saas' ),
'edit_item' => __( 'Edit Testimonial', 'saas' ),
'new_item' => __( 'New Testimonial', 'saas' ),
'view_item' => __( 'View Testimonial', 'saas' ),
'search_items' => __( 'Search Testimonials', 'saas' ),
'not_found' => __( 'No testimonials found', 'saas' ),
'not_found_in_trash' => __( 'No testimonials found in Trash', 'saas' ),
),
);
register_post_type( 'testimonial', $args );
/* Register new image size for the testimonials */
add_image_size( 'testimonial-thumbnail', 175, 128, true );
}
public function pico_add_meta_boxes() {
add_meta_box( 'pico_testimonials_meta_box', __( 'Testimonial Details' ), array( &$this, 'pico_testimonials_meta_box'), 'testimonial', 'side', 'core' );
}
public function pico_testimonials_meta_box() {
global $post_ID;?>
<div id="testimonial_meta">
<?php wp_nonce_field( plugin_basename( __FILE__ ), 'pico_testimonial_nonce' );
$business_name = get_post_meta( $post_ID, 'business_name', true );
// Business Name ?>
<p>
<label for="business_name" style="display:inline-block;"><?php _e( "Business Name:" ); ?></label>
<input type="text" id="business_name" name="business_name" value="<?php esc_attr_e( $business_name ); ?>" size="25" />
</p>
</div>
<?php
}
public function pico_save_testimonial_meta() {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( empty( $_POST[ 'pico_testimonial_nonce' ] ) || ! wp_verify_nonce( $_POST[ 'pico_testimonial_nonce' ], plugin_basename( __FILE__ ) ) )
return;
update_post_meta( $_POST[ 'ID' ], 'business_name', sanitize_text_field( $_POST[ 'business_name' ] ) );
}
public function pico_testimonials_add_meta_boxes() {
remove_meta_box( 'postimagediv', 'testimonial', 'side' );
add_meta_box( 'postimagediv', 'Testimonial Image', 'post_thumbnail_meta_box', 'testimonial', 'side' );
}
public function pico_testimonials_post_thumbnail_html( $output ) {
global $post_type;
// beware of translated admin
if ( ! empty ( $post_type ) && 'testimonial' == $post_type ) {
$output = str_replace( 'Set featured image', 'Select / Upload a testimonial image', $output );
$output = str_replace( 'Remove featured image', 'Remove testimonial image', $output );
}
return $output;
}
public function pico_testimonial_updated_messages( $messages ) {
global $post, $post_ID;
$messages['testimonial'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __( 'Testimonial updated. <a href="%s">View testimonial</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __( 'Custom field updated.' ),
3 => __( 'Custom field deleted.' ),
4 => __( 'Testimonial updated.' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET[ 'revision' ]) ? sprintf( __( 'Testimonial restored to revision from %s' ), wp_post_revision_title( (int) $_GET[ 'revision' ], false ) ) : false,
6 => sprintf( __( 'Testimonial published. <a href="%s">View testimonial</a>'), esc_url( get_permalink( $post_ID ) ) ),
7 => __( 'Testimonial saved.' ),
8 => sprintf( __( 'Testimonial submitted. <a target="_blank" href="%s">Preview testimonial</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __( 'Testimonial scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview testimonial</a>' ),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ) ),
10 => sprintf( __( 'Testimonial draft updated. <a target="_blank" href="%s">Preview testimonial</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) ),
);
return $messages;
}
}
$pico_testimonial = new Pico_Testimonials();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment