// ساخت post type دیدگاه های مشتریان
add_action( 'init', 'testimonials_post_type' );
function testimonials_post_type() {
$labels = array(
'name' => 'دیدگاه های مشتریان',
'singular_name' => 'Testimonial',
'add_new' => 'افزودن جدید',
'add_new_item' => 'افزودن دیدگاه جدید',
'edit_item' => 'ویرایش دیدگاه',
'new_item' => 'دیدگاه جدید',
'search_items' => 'جستجو دیدگاه',
'not_found' => 'دیدگاهی پیدا نشد',
'not_found_in_trash' => 'دیدگاهی در زباله دان پیدا نشد',
'parent_item_colon' => '',
);
register_post_type( 'testimonials', array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'exclude_from_search' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 10,
'supports' => array( 'editor' ),
'register_meta_box_cb' => 'testimonials_meta_boxes', // فراخوانی تابع متاباکس
) );
}
function testimonials_meta_boxes() {
add_meta_box( 'testimonials_form', 'جزئیات مشتری', 'testimonials_form', 'testimonials', 'normal', 'high' );
}
function testimonials_form() {
$post_id = get_the_ID();
$testimonial_data = get_post_meta( $post_id, '_testimonial', true );
$client_name = ( empty( $testimonial_data['client_name'] ) ) ? '' : $testimonial_data['client_name'];
$source = ( empty( $testimonial_data['source'] ) ) ? '' : $testimonial_data['source'];
$link = ( empty( $testimonial_data['link'] ) ) ? '' : $testimonial_data['link'];
$image = ( empty( $testimonial_data['image'] ) ) ? '' : $testimonial_data['image'];
wp_nonce_field( 'testimonials', 'testimonials' );
?>
$post_id,
'post_title' => 'Testimonial - ' . $post_id
) );
add_action( 'save_post', 'testimonials_save_post' );
}
if ( ! empty( $_POST['testimonial'] ) ) {
$testimonial_data['client_name'] = ( empty( $_POST['testimonial']['client_name'] ) ) ? '' : sanitize_text_field( $_POST['testimonial']['client_name'] );
$testimonial_data['source'] = ( empty( $_POST['testimonial']['source'] ) ) ? '' : sanitize_text_field( $_POST['testimonial']['source'] );
$testimonial_data['link'] = ( empty( $_POST['testimonial']['link'] ) ) ? '' : esc_url( $_POST['testimonial']['link'] );
$testimonial_data['image'] = ( empty( $_POST['testimonial']['image'] ) ) ? '' : esc_url( $_POST['testimonial']['image'] );
update_post_meta( $post_id, '_testimonial', $testimonial_data );
} else {
delete_post_meta( $post_id, '_testimonial' );
}
}
add_filter( 'manage_edit-testimonials_columns', 'testimonials_edit_columns' );
function testimonials_edit_columns( $columns ) {
$columns = array(
'cb' => '',
'title' => 'عنوان',
'testimonial' => 'دیدگاه مشتری',
'testimonial-client-name' => 'نام مشتری',
'testimonial-source' => 'بیزینس / سایت',
'testimonial-link' => 'لینک',
'author' => 'نویسنده',
'date' => 'تاریخ'
);
return $columns;
}
add_action( 'manage_posts_custom_column', 'testimonials_columns', 10, 2 );
function testimonials_columns( $column, $post_id ) {
$testimonial_data = get_post_meta( $post_id, '_testimonial', true );
switch ( $column ) {
case 'testimonial':
the_excerpt();
break;
case 'testimonial-client-name':
if ( ! empty( $testimonial_data['client_name'] ) )
echo $testimonial_data['client_name'];
break;
case 'testimonial-source':
if ( ! empty( $testimonial_data['source'] ) )
echo $testimonial_data['source'];
break;
case 'testimonial-link':
if ( ! empty( $testimonial_data['link'] ) )
echo $testimonial_data['link'];
break;
case 'testimonial-image':
if ( ! empty( $testimonial_data['image'] ) )
echo $testimonial_data['image'];
break;
}
}
function get_testimonial( $posts_per_page = 1, $orderby = 'none', $testimonial_id = null ) {
$args = array(
'posts_per_page' => (int) $posts_per_page,
'post_type' => 'testimonials',
'orderby' => $orderby,
'no_found_rows' => true,
);
if ( $testimonial_id )
$args['post__in'] = array( $testimonial_id );
$query = new WP_Query( $args );
$testimonials = '';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$testimonial_data = get_post_meta( $post_id, '_testimonial', true );
$client_name = ( empty( $testimonial_data['client_name'] ) ) ? '' : $testimonial_data['client_name'];
$source = ( empty( $testimonial_data['source'] ) ) ? '' : ' - ' . $testimonial_data['source'];
$link = ( empty( $testimonial_data['link'] ) ) ? '' : $testimonial_data['link'];
$image = ( empty( $testimonial_data['image'] ) ) ? '' : $testimonial_data['image'];
$cite = ( $link ) ? '' . $client_name . $source . '' : $client_name . $source;
$testimonials .= '';
endwhile;
wp_reset_postdata();
}
return $testimonials;
}
add_shortcode( 'testimonial', 'testimonial_shortcode' );
/**
* ساخت شورت کد
*
*/
function testimonial_shortcode( $atts ) {
extract( shortcode_atts( array(
'posts_per_page' => '1',
'orderby' => 'none',
'testimonial_id' => '',
), $atts ) );
return get_testimonial( $posts_per_page, $orderby, $testimonial_id );
}
/**
* ساخت ابزارک
*/
class Testimonial_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array( 'classname' => 'testimonial_widget', 'description' => 'نمایش دیدگاه های مشتریان' );
parent::__construct( 'testimonial_widget', 'دیدگاه های مشتریان', $widget_ops );
}
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$posts_per_page = (int) $instance['posts_per_page'];
$orderby = strip_tags( $instance['orderby'] );
$testimonial_id = ( null == $instance['testimonial_id'] ) ? '' : strip_tags( $instance['testimonial_id'] );
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
echo get_testimonial( $posts_per_page, $orderby, $testimonial_id );
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['posts_per_page'] = (int) $new_instance['posts_per_page'];
$instance['orderby'] = strip_tags( $new_instance['orderby'] );
$instance['testimonial_id'] = ( null == $new_instance['testimonial_id'] ) ? '' : strip_tags( $new_instance['testimonial_id'] );
return $instance;
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'posts_per_page' => '1', 'orderby' => 'none', 'testimonial_id' => null ) );
$title = strip_tags( $instance['title'] );
$posts_per_page = (int) $instance['posts_per_page'];
$orderby = strip_tags( $instance['orderby'] );
$testimonial_id = ( null == $instance['testimonial_id'] ) ? '' : strip_tags( $instance['testimonial_id'] );
?>