Last active
October 10, 2018 11:45
-
-
Save oterox/3bceb53fc6e7bce78846b1cbf1c1645e to your computer and use it in GitHub Desktop.
Custom rewrite rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('post_type_link', 'ox_post_type_link', 1, 3); | |
function ox_post_type_link( $link, $post = 0 ){ | |
if ( $post->post_type == 'plan' ){ | |
$terms = wp_get_object_terms( $post->ID, 'tax_destination' ); | |
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) | |
$taxonomy_slug = $terms[0]->slug; | |
return home_url( $taxonomy_slug .'/' . $post->post_name ); | |
} elseif ( $post->post_type == 'product' ){ | |
$terms = wp_get_object_terms( $post->ID, 'tax_destination' ); | |
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) | |
$taxonomy_slug = $terms[0]->slug; | |
return home_url( $taxonomy_slug .'/experiencias/' . $post->post_name ); | |
} elseif ( $post->post_type == 'area' ){ | |
$terms = wp_get_object_terms( $post->ID, 'tax_destination' ); | |
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) | |
$taxonomy_slug = $terms[0]->slug; | |
return home_url( $taxonomy_slug .'/zonas/' . $post->post_name ); | |
} elseif ( $post->post_type == 'itinerary' ){ | |
$terms = wp_get_object_terms( $post->ID, 'tax_destination' ); | |
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) | |
$taxonomy_slug = $terms[0]->slug; | |
return home_url( $taxonomy_slug .'/itinerarios/' . $post->post_name ); | |
} elseif ( $post->post_type == 'accommodation' ){ | |
$terms = wp_get_object_terms( $post->ID, 'tax_destination' ); | |
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) | |
$taxonomy_slug = $terms[0]->slug; | |
return home_url( $taxonomy_slug .'/alojamientos/' . $post->post_name ); | |
} else { | |
return $link; | |
} | |
} | |
add_filter('post_type_archive_link', 'ox_post_type_archive_link', 1, 2); | |
function ox_post_type_archive_link( $link, $post_type ) { | |
if( 'plan' == $post_type ){ | |
return home_url( $_SESSION['destino_slug'] . '/planes' ); | |
} | |
if( 'itinerary' == $post_type ){ | |
return home_url( $_SESSION['destino_slug'] . '/itinerarios' ); | |
} | |
if( 'accommodation' == $post_type ){ | |
return home_url( $_SESSION['destino_slug'] . '/alojamientos' ); | |
} | |
if( 'area' == $post_type ){ | |
return home_url( $_SESSION['destino_slug'] . '/zonas' ); | |
} | |
if( 'product' == $post_type ){ | |
return home_url( $_SESSION['destino_slug'] . '/experiencias' ); | |
} | |
return $link; | |
} | |
add_action( 'init', 'ox_rewrite_add_rewrites' ); | |
function ox_rewrite_add_rewrites() | |
{ | |
//Archivo para planes /granada/planes | |
add_rewrite_rule( | |
'^([^/]+)/planes/?$', //un segmento + el literal "planes" | |
'index.php?post_type=plan', | |
'top' | |
); | |
//Archivo para planes /granada/planes | |
add_rewrite_rule( | |
'^([^/]+)/itinerarios/?$', //un segmento + el literal "planes" | |
'index.php?post_type=itinerary', | |
'top' | |
); | |
//Archivo para planes /granada/planes | |
add_rewrite_rule( | |
'^([^/]+)/zonas/?$', //un segmento + el literal "planes" | |
'index.php?post_type=area', | |
'top' | |
); | |
//Archivo para experiencias /granada/experiencias | |
add_rewrite_rule( | |
'^([^/]+)/experiencias/?$', //un segmento + el literal "planes" | |
'index.php?post_type=product', | |
'top' | |
); | |
//Archivo para alojamientos /granada/alojamientos | |
add_rewrite_rule( | |
'^([^/]+)/alojamientos/?$', | |
'index.php?post_type=accommodation', | |
'top' | |
); | |
//Home destino /granada | |
add_rewrite_rule( | |
'^([^/]+)/?$', //un solo segmento | |
'index.php?post_type=destination&destination=$matches[1]', | |
'top' | |
); | |
//Detalle plan /granada/que-hacer-en-granada | |
add_rewrite_rule( | |
'^([^/]+)/([^/]+)/?$', //un segmento y despues otro segmento | |
'index.php?plan=$matches[2]', | |
'top' | |
); | |
//Detalle zona /granada/zonas/albaicin | |
add_rewrite_rule( | |
'^([^/]+)/zonas/([^/]+)/?$', //un segmento y despues otro segmento | |
'index.php?area=$matches[2]', | |
'top' | |
); | |
//Detalle itinerario /granada/itinerarios/granada-en-un-dia | |
add_rewrite_rule( | |
'^([^/]+)/itinerarios/([^/]+)/?$', //un segmento y despues otro segmento | |
'index.php?itinerary=$matches[2]', | |
'top' | |
); | |
//Detalle experiencia /granada/experiencias/granada-en-globo | |
add_rewrite_rule( | |
'^([^/]+)/experiencias/([^/]+)/?$', //un segmento y despues otro segmento | |
'index.php?product=$matches[2]', | |
'top' | |
); | |
flush_rewrite_rules(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment