Last Updated: October 21, 2019
Complete tutorial – Coming Soon
function custom_rewrite_basic() { add_rewrite_rule('^products/([^/]*)/reviews/([^/]*)', 'index.php?product-slug=$matches[1]&product-child=reviews&product-comment-key=$matches[2]', 'top'); } add_action('init', 'custom_rewrite_basic'); function prefix_register_query_var( $vars ) { $vars[] = 'product-slug'; $vars[] = 'product-child'; $vars[] = 'product-comment-key'; return $vars; } add_filter( 'query_vars', 'prefix_register_query_var' ); function prefix_url_rewrite_templates() { if ( get_query_var( 'product-slug' ) && get_query_var( 'product-child' ) == 'reviews' && get_query_var( 'product-comment-key' )) { $product_slug = get_query_var( 'product-slug' ); $product_page = get_page_by_path($product_slug, OBJECT, 'product' ); $product_id = $product_page->ID; $product_comment_key = get_query_var( 'product-comment-key' ); $product_comment_meta = explode('-', $product_comment_key); $comment_id = end($product_comment_meta); $comment_data = get_comment( $comment_id); $comment_status = wp_get_comment_status($comment_id); if ($comment_status == 'approved'): $other_comments = array(); $other_comments = get_comments(array( 'post_id'=> $product_page->ID, 'number' => '', 'status' => 'approve', 'orderby' => 'rand' )); shuffle($other_comments); $GLOBALS['comments'] = $other_comments; $other_comment_data = $other_comments[0]; $other_comment_id = $other_comment_data->comment_ID; $other_comment = get_comment($other_comment_id, OBJECT); $comment_content = strip_tags($comment_data->comment_content . $comment->comment_content); if (count($comment_count) < 160){ $comment_content.= ' ' . $other_comment->comment_content; } $comment_content = strip_tags($comment_content); $comment_content = substr($comment_content,0,155); if (count($comment_content) < 160){ $other_comment_data = $other_comments[1]; $other_comment_id = $other_comment_data->comment_ID; $other_comment = get_comment($other_comment_id, OBJECT); $comment_content.= ' ' . $other_comment->comment_content; } $comment_content = strip_tags($comment_content); $comment_content = substr($comment_content,0,155); $GLOBALS['seo_title'] = $product_page->post_title . ' - Review #' . $comment_id . ' By ' . $comment_data->comment_author; $GLOBALS['seo_description'] = substr($comment_data->comment_content,0,155); $GLOBALS['seo_keyword'] = $product_page->post_title . ' - Review #' . $comment_id; $GLOBALS['current_url'] = get_permalink($product_id) . 'reviews/' . get_query_var( 'product-comment-key' ); $GLOBALS['seo_image'] = get_the_post_thumbnail_url($product_id,'full');; add_filter('wpseo_title', function($seo_title){ return $GLOBALS['seo_title']; },100); add_filter('wpseo_metadesc', function($seo_title){ return $GLOBALS['seo_description']; },100); add_filter('wpseo_metakey', function($seo_title){ return $GLOBALS['seo_keyword']; },100); add_filter('wpseo_opengraph_url', function($seo_title){ return $GLOBALS['current_url']; },100); add_filter('wpseo_opengraph_image',function(){ return $GLOBALS['seo_image'] ; },100); add_filter('wpseo_twitter_image',function(){ return $GLOBALS['seo_image'] ; },100); add_filter( 'template_include', function() { return get_template_directory() . '/page-templates/page-product-comments-single.php'; }); else: add_filter( 'template_include', function() { return get_template_directory() . '/404.php'; }); endif; } } add_action( 'template_redirect', 'prefix_url_rewrite_templates' );