Removing parent slug in hierachial custom post type

I was able to solve this problem by adding the code below under my theme/functions.php file. (My custom post type slug is ‘news’)

            function df_custom_post_type_link( $post_link, $id = 0 ) {  
                $post = get_post($id);  
                if ( is_wp_error($post) || 'news' != $post->post_type || empty($post->post_name) )  
                    return $post_link;  
                return home_url(user_trailingslashit( "$post->post_name" ));  
            }
            add_filter( 'post_type_link', 'df_custom_post_type_link' , 10, 2 );
            function df_custom_rewrite_rule() {
                add_rewrite_rule('(.*?)$', 'index.php?news=$matches[1]', 'top');
            }
            add_action('init', 'df_custom_rewrite_rule');