Skip to content

Instantly share code, notes, and snippets.

@islandjoe
Last active March 11, 2021 14:24
Show Gist options
  • Save islandjoe/83ec3fe939b6c94b9bc028bc0f390ac9 to your computer and use it in GitHub Desktop.
Save islandjoe/83ec3fe939b6c94b9bc028bc0f390ac9 to your computer and use it in GitHub Desktop.
WordPress Review custom post type
<?php
/*
Plugin Name: Review Custom Post Type
Plugin URI: http://wanderlusting.me
Description: Describe what your plugin does and why you created it.
Version: 1.0
Author: Arthur Kho
Author URI: https://github.com/islandjoe
*/
// Register the custom post type function.
add_action( 'init', function () {
// Rewrite the permalink structure so that instead of:
// /lifeinthephilippines/review/hotel/the-post
// it becomes:
// /lifeinthephilippines/hotel-review/the-post
global $wp_rewrite;
$review_structure = '/%review-type%-review/%review%';
$wp_rewrite->add_rewrite_tag("%review%", '([^/]+)', "review=");
$wp_rewrite->add_permastruct('review', $review_structure, false);
/**
* Register the 'review' post type.
* @link http://codex.wordpress.org/Function_Reference/register_post_type
**/
register_post_type( 'review', [
'labels' => [
'name' => _x( 'Reviews', 'general name for the post type, usually plural' ),
'singular_name' => _x( 'Review', 'name for one object of this post type' ),
'add_new' => _x( 'Add New', 'review' ),
'add_new_item' => __( 'Add New Review' ),
'edit_item' => __( 'Edit Review' ),
'new_item' => __( 'New Review' ),
'all_items' => __( 'All Reviews' ),
'view_item' => __( 'View Review' ),
'search_items' => __( 'Search Reviews' ),
'not_found' => __( 'No reviews found' ),
'not_found_in_trash' => __( 'No reviews found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Review'
],
'menu_icon' => 'dashicons-star-half',
'description' => "What the custom post type is about. What it does and why you're using it.",
'public' => true,
'menu_position' => 5,
'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt', 'trackbacks', 'comments', 'shortlinks', 'author' ],
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => false
] );
} ); // add_action('init')
//-- Meta box code below (e.g., add_action('admin_init')) --//
/* IMPORTANT!
Do not forget to update the $fields_id array
in 'save_post' action with the metabox's id */
// Meta Box for name of reviewed thing
add_action( 'admin_init', function() {
$id = 'name-metabox';
$title = __( 'Name', 'wme-lifeph' );
$post_type = 'review';
$context = 'normal';
$priority = 'default';
$callback = function($review) {
echo '<input type="text" name="name" id="name" class="widefat" value="'.
esc_html( get_post_meta( $review->ID, 'name', true ) ) .'" />';
};
add_meta_box( $id, $title, $callback, $post_type, $context, $priority );
});
// Meta Box for Rating
add_action( 'admin_init', function() {
$id = 'rating-metabox';
$title = __( 'Rating', 'wme-lifeph' );
$post_type = 'review';
$context = 'normal';
$priority = 'default';
$callback = function($review) {
$rating = intval( get_post_meta( $review->ID, 'rating', true ));
echo '<select name="rating" id="rating">';
foreach ([1,2,3,4,5] as $n):
echo '<option value="', $n, '" ', selected( $rating, $n ), '>', $n;
endforeach;
echo '</select>';
echo ' out of 5';
};
add_meta_box( $id, $title, $callback, $post_type, $context, $priority );
});
// Meta Box for PROS and CONS
add_action( 'admin_init', function() {
$id = 'pros-metabox';
$title = __( 'PROS', 'wme-lifeph' );
$post_type = 'review';
$context = 'normal';
$priority = 'default';
$callback = function($review) {
echo '<textarea name="pros" id="pros" rows="5" style="width:100%">',
esc_html( get_post_meta( $review->ID, 'pros', true ) ) ,
'</textarea>';
};
add_meta_box( $id, $title, $callback, $post_type, $context, $priority );
});
add_action( 'admin_init', function() {
$id = 'cons-metabox';
$title = __( 'CONS', 'wme-lifeph' );
$post_type = 'review';
$context = 'normal';
$priority = 'default';
$callback = function($review) {
echo '<textarea name="cons" id="cons" rows="5" style="width:100%">',
esc_html( get_post_meta( $review->ID, 'cons', true ) ) ,
'</textarea>';
};
add_meta_box( $id, $title, $callback, $post_type, $context, $priority );
});
add_action( 'admin_head', function() {
global $post;
$metabox_review = function () {
$id = 'review-metabox';
$title = __( 'Ratings', 'wme-lifeph' );
$post_type = 'review';
$context = 'normal';
$priority = 'default';
$callback = function($review) {
$location = intval( get_post_meta( $review->ID, 'location-rating', true ) );
$ambience = intval( get_post_meta( $review->ID, 'ambience-rating', true ) );
$service = intval( get_post_meta( $review->ID, 'service-rating', true ) );
$cleanliness = intval( get_post_meta( $review->ID, 'cleanliness-rating', true ) );
$value = intval( get_post_meta( $review->ID, 'value-rating', true ) );
$labels = ['Location'=>$location, 'Ambience'=>$ambience, 'Service'=>$service, 'Cleanliness'=>$cleanliness, 'Value'=>$value];
echo '<table width="100%">';
foreach ($labels as $label=>$rating):
$id = sprintf("%s-rating", strtolower($label) );
echo '<tr>',
'<td>', $label, '</td>';
echo '<td style="text-align:right">',
' <select name="', $id, '" id="', $id, '">';
foreach ([1,2,3,4,5] as $n):
echo ' <option value="', $n, '" ', selected( $rating, $n ), '>', $n;
endforeach;
echo '</select>',
'</td>',
'</tr>';
endforeach;
?>
</table><?
};
add_meta_box( $id, $title, $callback, $post_type, $context, $priority );
};
$terms = get_the_terms($post->ID, 'review-type');
if ( 'hotel' == $terms[0]->slug ) {
add_action( 'admin_init', $metabox_review() );
}
} );
add_action( 'save_post', function( $review_id, $review) {
if ( 'review' == $review->post_type ) {
$doUpdate = function( $field_id ) use ( $review_id ) {
if ( isset( $_POST[ $field_id ] ) && $_POST[ $field_id ] !== '' ) {
update_post_meta( $review_id, $field_id, $_POST[ $field_id ] );
}
}; //$doUpdate
$fields_id = ['name',
'rating',
'location-rating',
'ambience-rating',
'service-rating',
'cleanliness-rating',
'value-rating',
'pros',
'cons'];
foreach ($fields_id as $field) {
if ( isset( $_POST[ $field ] ) && $_POST[ $field ] !== '' ) {
update_post_meta( $review_id, $field, $_POST[ $field ] );
}
} //foreach
}
}, 10, 2 );
// Taxonomy
add_action( 'init', function () {
register_taxonomy(
'review-type', //Taxonomy name
'review', //Custom post type
array(
'labels' => array(
'name' => 'Review Type',
'add_new_item' => 'Add New Review Type',
'new_item_name' => "New Review Type"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'query_var' => 'review-type',
'rewrite' => true
)
);
}, 0 );
foreach (['post_link', 'post_type_link'] as $v) {
add_filter( $v, function ($permalink, $post_id, $leavename) {
if (strpos($permalink, '%review-type%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'review-type');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'generic';
return str_replace('%review-type%', $taxonomy_slug, $permalink);
} , 10, 3);
}
// Tags
add_action( 'init', function () {
register_taxonomy(
'review-tags',
'review',
array(
'labels' => array(
'name' => 'Tags',
'add_new_item' => 'Add New Tags',
'new_item_name' => "New Tag"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => false
)
);
}, 0 );
// Force the use of our dedicated template
add_filter( 'template_include', function( $template_path ) {
$post_type = 'review';
$template_file_name = sprintf("single-%s.php", $post_type);
if ( get_post_type() == $post_type ) {
if ( is_single() ) {
// Check first if the file exists in the theme...
// otherwise serve the file from the plugin.
if ( $theme_file = locate_template( [ $template_file_name ] ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . $template_file_name;
}
}
}
return $template_path;
}, 1 );
// Display review posts on the home page
add_filter( 'pre_get_posts', function($query) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'review' ) );
return $query;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment