How to overwrite registered taxonomy url from vendor plugin in child theme
Question
In a vendor plugin I got this :
register_taxonomy( 'listing-region', 'listing', array( 'rewrite' => false, 'hierarchical' => true, 'label' => __( 'Regions', 'bt_plugin' ), 'singular_name' => __( 'Region', 'bt_plugin' ), 'show_admin_column' => true ) );
To avoid to lock plugin update, I would like to overwrite the ‘rewrite’ parameter in the child theme to get this :
https://www.myexample.com/region/foo <=> https://www.myexample.com?listing-region=foo
How can I do this ?
For the moment I tried this in functions.php :
function theme_child_custom_rewrite() {
//Ensure the $wp_rewrite global is loaded
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules( false );
add_rewrite_tag('%listing-region%','([^&]+)');
add_rewrite_rule('^region/(.*?)$', 'index.php?listing-region=$matches[1]', 'top');
}
add_action('init','theme_child_custom_rewrite', 1000);
function listing_region_term_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'course' );
if( $terms ){
return str_replace( '%listing-region%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'term_link', 'listing_region_term_link', 1, 3 );
0
custom-taxonomy, rewrite-rules, url-rewriting
4 years
2020-03-26T08:50:54-05:00
2020-03-26T08:50:54-05:00 0 Answers
120 views
0
Leave an answer