Skip to main content

ACF – Elimina tassonomia dal permalink

Elimina una tassonomia


add_filter('request', 'wptwpm_remove_term_request', 1, 1 );

function wptwpm_remove_term_request($query){
	
	$tax_name = 'course-category'; // specify your taxonomy name here
	
	// Request for child terms differs, we should make an additional check
	if( $query['attachment'] ) :
		$include_children = true;
		$name = $query['attachment'];
	else:
		$include_children = false;
		$name = $query['name'];
	endif;
	
	
	$term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists
	
	if (isset($name) && $term && !is_wp_error($term)): // check it here
		
		if( $include_children ) {
			unset($query['attachment']);
			$parent = $term->parent;
			while( $parent ) {
				$parent_term = get_term( $parent, $tax_name);
				$name = $parent_term->slug . '/' . $name;
				$parent = $parent_term->parent;
			}
		} else {
			unset($query['name']);
		}
		
		switch( $tax_name ):
			case 'category':{
				$query['category_name'] = $name; // for categories
				break;
			}
			case 'post_tag':{
				$query['tag'] = $name; // for post tags
				break;
			}
			default:{
				$query[$tax_name] = $name; // for another taxonomies
				break;
			}
		endswitch;

	endif;
	
	return $query;
	
}


add_filter( 'term_link', 'wptwpm_change_term_permalink', 10, 3 );

function wptwpm_change_term_permalink( $url, $term, $taxonomy ){
	
	$taxonomy_name = 'course-category'; // your taxonomy name here
	$taxonomy_slug = 'course-category'; // the taxonomy slug can be different with the taxonomy name

	// exit the function if taxonomy slug is not in URL
	if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url;
	
	$url = str_replace('/' . $taxonomy_slug, '', $url);
	
	return $url;
}


add_action('template_redirect', 'wptwpm_old_term_redirect');

function wptwpm_old_term_redirect() {
	
	$taxonomy_name = 'course-category'; // your taxonomy name here
	$taxonomy_slug = 'course-category'; // your taxonomy slug here
	
	// exit the redirect function if taxonomy slug is not in URL
	if( strpos( $_SERVER['REQUEST_URI'], $taxonomy_slug ) === FALSE)
		return;

	if( ( is_category() && $taxonomy_name=='category' ) || ( is_tag() && $taxonomy_name=='post_tag' ) || is_tax( $taxonomy_name ) ) :

        	wp_redirect( site_url( str_replace($taxonomy_slug, '', $_SERVER['REQUEST_URI']) ), 301 );
		exit();
		
	endif;

}

Elimina due tassonomie

<?php

/* GENERE */
add_filter('request', 'wptwpm_remove_term_request', 1, 1);

function wptwpm_remove_term_request($query)
{
	$taxes = ['type', 'genere'];
	//$tax_name = 'genere'; // specify your taxonomy name here

	// Request for child terms differs, we should make an additional check
	if ($query['attachment']) :
		$include_children = true;
		$name = $query['attachment'];
	else :
		$include_children = false;
		$name = $query['name'];
	endif;

	foreach ($taxes as $tax_name) {

		$term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists

		if (isset($name) && $term && !is_wp_error($term)) : // check it here

			if ($include_children) {
				unset($query['attachment']);
				$parent = $term->parent;
				while ($parent) {
					$parent_term = get_term($parent, $tax_name);
					$name = $parent_term->slug . '/' . $name;
					$parent = $parent_term->parent;
				}
			} else {
				unset($query['name']);
			}

			switch ($tax_name):
				case 'category': {
						$query['category_name'] = $name; // for categories
						break;
					}
				case 'post_tag': {
						$query['tag'] = $name; // for post tags
						break;
					}
				default: {
						$query[$tax_name] = $name; // for another taxonomies
						break;
					}
			endswitch;

		endif;
	}

	return $query;
}


add_filter('term_link', 'wptwpm_change_term_permalink', 10, 3);

function wptwpm_change_term_permalink($url, $term, $taxonomy)
{
	$taxes = ['type', 'genere'];
	foreach ($taxes as $tax) {
		$taxonomy_name = $tax; // your taxonomy name here
		$taxonomy_slug = $tax; // the taxonomy slug can be different with the taxonomy name
		// exit the function if taxonomy slug is not in URL
		if (strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name) return $url;
		$url = str_replace('/' . $taxonomy_slug, '', $url);
	}
	return $url;
}


add_action('template_redirect', 'wptwpm_old_term_redirect');

function wptwpm_old_term_redirect()
{
	// die("kkk");
	$obj = get_queried_object();
	// print_r($obj);
	// die;
	// if ((is_category('genere') && (isset($obj->taxonomy) && 'genere' == $obj->taxonomy))) {
	// 	die("kkk");
	// }
	$taxes = ['type', 'genere'];

	foreach ($taxes as $tax) {
		$taxonomy_name = $tax; // your taxonomy name here
		$taxonomy_slug = $tax; // your taxonomy slug here

		// exit the redirect function if taxonomy slug is not in URL
		if (strpos($_SERVER['REQUEST_URI'], $taxonomy_slug) === FALSE)
			continue;

		if ((isset($obj->taxonomy) && ('genere' == $obj->taxonomy || 'type' == $obj->taxonomy))) :
			// die("kkk");
			wp_redirect(site_url(str_replace($taxonomy_slug, '', $_SERVER['REQUEST_URI'])), 301);
			exit();

		endif;
	}
}
Torna su
×