Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Display second level term (product category) in permalinks & breadcrumbs
- */
- //ini_set('display_errors', 1);
- //ini_set('display_startup_errors', 1);
- //error_reporting(E_ALL);
- function pm_get_product_term($post = null, $last_level_term = null) {
- if(!empty($post)) {
- $post = (is_numeric()) ? get_post($post) : $post;
- // Get last-level terms ancestors
- $post_terms = (array) wp_get_post_terms($post->ID, 'product_cat', array('orderby' => 'term_id'));
- $last_level_term = end($post_terms);
- // Use last-levet term in permalinks & breadcrumbs if the product is assigned to 'programmable-dmx-controllers' category
- if(has_term(array('programmable-dmx-controllers', 'stand-alone-dmx-controllers'), 'product_cat', $post)) {
- return $last_level_term;
- }
- }
- // Stop if no term set
- if(empty($last_level_term->term_id)) { return ''; }
- $ancestors = get_ancestors($last_level_term->term_id, 'product_cat', 'taxonomy');
- // How many levels we have?
- $count = count($ancestors);
- if($count >= 1) {
- $replacement_term = get_term($ancestors[0], 'product_cat');
- }
- return (!empty($replacement_term)) ? $replacement_term : '';
- }
- function pm_shorten_category_base($replacement, $term, $element) {
- if(!empty($element->ID) && $term->taxonomy == 'product_cat') {
- // Get last-level terms ancestors
- $replacement_term = pm_get_product_term($post);
- $replacement = $replacement_term->slug;
- } else {
- $replacement_term = pm_get_product_term(null, $term);
- $replacement = "{$replacement_term->slug}/{$term->slug}";
- }
- return $replacement;
- }
- add_filter('permalink_manager_filter_term_slug', 'pm_shorten_category_base', 9, 3);
- add_filter('permalink_manager_filter_default_term_slug', 'pm_shorten_category_base', 9, 3);
- function pm_filter_breadcrumbs($trail) {
- global $post, $wp;
- $breadcrumbs = $trail->breadcrumbs;
- if(is_product()) {
- $new_breadcrumbs[0] = $breadcrumbs[0];
- if(strpos(trim($wp->request, '/'), '/') !== false) {
- $second_level_term = pm_get_product_term($post);
- if(!empty($second_level_term->term_id)) {
- $new_breadcrumbs[1] = new bcn_breadcrumb($second_level_term->name, null, array(), get_term_link($second_level_term->term_id, $second_level_term->taxonomy), $second_level_term->term_id);
- }
- }
- } else if(is_product_category()) {
- $new_breadcrumbs[0] = $breadcrumbs[0];
- if(strpos(trim($wp->request, '/'), '/') !== false) {
- $second_level_term = pm_get_product_term(null, get_queried_object());
- if(!empty($second_level_term->term_id)) {
- $new_breadcrumbs[1] = new bcn_breadcrumb($second_level_term->name, null, array(), get_term_link($second_level_term->term_id, $second_level_term->taxonomy), $second_level_term->term_id);
- }
- }
- }
- // Set new breadcrumbs
- if(!empty($new_breadcrumbs)) {
- $trail->breadcrumbs = $new_breadcrumbs;
- }
- }
- add_action('bcn_after_fill', 'pm_filter_breadcrumbs');
- function pm_redirect_posts_from_404() {
- global $wp, $wpdb, $wp_query, $pm_query;
- if(is_front_page() || is_home()) {
- return;
- }
- if(is_404() || (empty($pm_query))) {
- $slug = basename($wp->request);
- $element = $wpdb->get_row($wpdb->prepare("
- SELECT * FROM {$wpdb->posts}
- WHERE post_name = %s
- AND post_status = 'publish'
- ",
- $slug
- ));
- if(!empty($element->ID)) {
- $url = get_permalink($element->ID);
- }
- if(!empty($url)) {
- wp_safe_redirect($url, 301);
- exit();
- }
- }
- }
- add_action('template_redirect', 'pm_redirect_posts_from_404', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement