Modify custom post type rewrite rules in a separate function

I want to modify the rewrite rules for a custom post type, tribe_events, which is registered as a post type within “The Events Calendar” core plugin files (the-events-calendar.class line 24):

    protected $postTypeArgs = array(
        'public' => true,
        'rewrite' => array('slug' => 'event', 'with_front' => false),
        'menu_position' => 6,
        'supports' => array('title','editor','excerpt','author','thumbnail', 'custom-fields'),
        'capability_type' => array('tribe_event', 'tribe_events'),
        'map_meta_cap' => true
    );

What I would like to do is modify the line:

        'rewrite' => array('slug' => 'event', 'with_front' => false)

To:

‘rewrite’ => array(‘slug’ => ‘event/%lugares%’, ‘with_front’ => false)

Where “%lugares%” is the name of a custom taxonomy.

Then my plan is to use the following function to complete the rewrite process:

add_filter('post_type_link', 'events_permalink_structure', 10, 4);
function events_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, '%lugares%' ) ) {
        $lugares_term = get_the_terms( $post->ID, 'lugares' );
        $post_link = str_replace( '%lugares%', array_pop( $lugares_term )->slug, $post_link );
    }
    return $post_link;
}

The issue is that I don’t want to modify the core plugin files. Is there a way to modify the custom post type rewrite argument in a seperate function from inside my theme’s functions.php??

Thanks!

1 Answer
1

Yes, I believe you can. Paste this code into your theme’s functions.php file:

function change_tribe_events_rewrite_rules() {
    global $wp_post_types;
    $rewrite = &$wp_post_types['tribe_events']->rewrite;
    $rewrite['slug'] = 'event/%lugares%';
}
add_action( 'init', 'change_tribe_events_rewrite_rules', 999 );

Leave a Comment