function create_custom_post_type_and_taxonomy() {
    // Register Custom Post Type
    register_post_type( 'testmonial',
        array(
            'labels' => array(
                'name' => __( 'Testmonials' ),
                'singular_name' => __( 'Testmonial' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'testmonials/%testmonials_cat%'),
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt')
        )
    );

    // Register Custom Taxonomy
    register_taxonomy(
        'testmonials_cat', 
        'testmonial', 
        array(
            'label' => __( 'Testmonials Categories' ),
            'rewrite' => array( 'slug' => 'testmonials-cat' ),
            'hierarchical' => true,
        )
    );

    // Flush rewrite rules on activation
    flush_rewrite_rules();
}

add_action( 'init', 'create_custom_post_type_and_taxonomy' );

// Replace placeholder with actual category
function change_testmonials_permalink( $post_link, $post ) {
    if ( is_object( $post ) && $post->post_type == 'testmonial' ) {
        $terms = wp_get_object_terms( $post->ID, 'testmonials_cat' );
        if ( $terms ) {
            return str_replace( '%testmonials_cat%', $terms[0]->slug, $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'change_testmonials_permalink', 10, 2 );