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 );

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/how-to-overwrite-registered-taxonomy-url-from-vendor-plugin-in-child-theme/">

How to overwrite registered taxonomy url from vendor plugin in child theme</a>
0
, , J.BizMai 2 months 0 Answers 71 views 0

Leave an answer