Created
March 10, 2015 22:34
-
-
Save xavortm/c1acfb8c16a3787e7c89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: IRIS - Testimonial custom post type | |
* Description: Set all testimonials as custom posts. | |
* Version: 1.0.0 | |
* Author: Alex Dimitrov | |
* Author URI: http://www.xavortm.com | |
* License: A short license name. Example: GPL2 | |
*/ | |
if ( ! class_exists('Iris_Testimonials') ) { | |
class Iris_Testimonials { | |
/** | |
* Construct - run all functions needed | |
*/ | |
function __construct() { | |
// Register the post type | |
$this->register_post_type(); | |
$this->add_actions(); | |
add_image_size( 'testimonial_thumbnail', 64, 64, true ); | |
} | |
function register_post_type() { | |
$labels = array( | |
'name' => 'Testimonials', | |
'menu_name' => 'Testimonials', | |
'name_admin_bar' => 'Testimonials', | |
'singular_name' => 'Testimonial', | |
'add_new' => 'New Testimonial', | |
'add_new_item' => 'New Testimonial', | |
'new_item' => 'New Testimonial', | |
'edit_item' => 'Edit Testimonial', | |
'view_item' => 'View Testimonial', | |
'search_items' => 'Search Testimonials', | |
'not_found' => 'Not found', | |
'not_found_in_trash' => 'Not found in trash' | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Custom testimonials to list on the homepage', | |
'public' => true, | |
'menu_position' => 5, | |
'rewrite' => false, | |
'supports' => array( 'editor', 'thumbnail' ) | |
); | |
register_post_type( 'testimonial', $args ); | |
} | |
/** | |
* Basicly hook the plugin to WordPress | |
*/ | |
function add_actions() { | |
add_action( 'init', array( $this, 'register_post_type' ) ); | |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); | |
add_action( 'save_post', array( $this, 'save' ) ); | |
} | |
/** | |
* Metaboxes for the needed content. | |
* @param [type] $post_type [description] | |
*/ | |
public function add_meta_box( $post_type ) { | |
$post_types = array('testimonial'); //limit meta box to certain post types | |
if ( in_array( $post_type, $post_types )) { | |
add_meta_box( | |
'some_meta_box_name' | |
,__( 'Set testimonial data', 'iris' ) | |
,array( $this, 'render_meta_box_content' ) | |
,$post_type | |
,'side' | |
,'default' | |
); | |
} | |
} | |
/** | |
* Save the meta when the post is saved. | |
* | |
* @param int $post_id The ID of the post being saved. | |
*/ | |
public function save( $post_id ) { | |
/* | |
* We need to verify this came from the our screen and with proper authorization, | |
* because save_post can be triggered at other times. | |
*/ | |
// Check if our nonce is set. | |
if ( ! isset( $_POST['testimonial_box_nonce'] ) ) | |
return $post_id; | |
$nonce = $_POST['testimonial_box_nonce']; | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $nonce, 'testimonial_box' ) ) | |
return $post_id; | |
// If this is an autosave, our form has not been submitted, | |
// so we don't want to do anything. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return $post_id; | |
// Check the user's permissions. | |
if ( 'page' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) | |
return $post_id; | |
} else { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) | |
return $post_id; | |
} | |
/* OK, its safe for us to save the data now. */ | |
// Sanitize the user input. | |
$author = sanitize_text_field( $_POST['input_author'] ); | |
$position = sanitize_text_field( $_POST['input_position'] ); | |
// Update the meta field. | |
update_post_meta( $post_id, '_author', $author ); | |
update_post_meta( $post_id, '_position', $position ); | |
} | |
/** | |
* Render Meta Box content. | |
* | |
* @param WP_Post $post The post object. | |
*/ | |
public function render_meta_box_content( $post ) { | |
// Add an nonce field so we can check for it later. | |
wp_nonce_field( 'testimonial_box', 'testimonial_box_nonce' ); | |
// Use get_post_meta to retrieve an existing value from the database. | |
$author = get_post_meta( $post->ID, '_author', true ); | |
$position = get_post_meta( $post->ID, '_position', true ); | |
// Display the form, using the current value. | |
echo '<p>'; | |
echo '<label for="input_author">'; | |
_e( 'Author', 'iris' ); | |
echo '</label> '; | |
echo '<input type="text" id="input_author" name="input_author" class="widefat"'; | |
echo ' value="' . esc_attr( $author ) . '" size="25"'; | |
echo '</p>'; | |
echo '<p>'; | |
echo '<label for="input_position">'; | |
_e( 'Position', 'iris' ); | |
echo '</label> '; | |
echo '<input type="text" id="input_position" name="input_position" class="widefat"'; | |
echo ' value="' . esc_attr( $position ) . '" size="25" />'; | |
echo '</p>'; | |
} | |
} // end Iris_Testimonials class | |
} | |
$obj = new Iris_Testimonials(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment