Adding a rewrite rule to a custom post type breaks Divi Theme Builder

78 Views Asked by At

I am building out a business listings site and the client wants the city to be in the URL. So I created a rewrite rule to do this and it works great. The problem is now my Theme Builder doesn't work. If I comment out the add_rewrite_rule line of code Theme Builder works fine.

I have tried Divi Safe Mode and Theme Builder works. Of course it does because there's no rewrite rules. I also tried my child theme with zero plugins enabled, fully updated WordPress and Divi, but still the Theme Builder does not work if the add_rewrite_rule code is active.

Here is the code in question. I installed this code on a completely different site and had the same issue.

$slug = 'listings';
$Title = 'Listing';
$Titles = 'Listings';
$titles = 'listings';

$labels = [
    'name'                       => _x($Titles, 'Taxonomy General Name', $text_domain),
    'singular_name'              => _x($Title, 'Taxonomy Singular Name', $text_domain),
    'menu_name'                  => __($Titles, $text_domain),
    'all_items'                  => __('All ' . $Titles, $text_domain),
    'parent_item'                => __('Parent ' . $Title, $text_domain),
    'parent_item_colon'          => __('Parent ' . $Title . ':', $text_domain),
    'new_item_name'              => __('New ' . $Title . ' Name', $text_domain),
    'add_new_item'               => __('Add New ' . $Title, $text_domain),
    'edit_item'                  => __('Edit ' . $Title, $text_domain),
    'update_item'                => __('Update ' . $Title, $text_domain),
    'view_item'                  => __('View ' . $Title, $text_domain),
    'separate_items_with_commas' => __('Separate ' . $titles . ' with commas', $text_domain),
    'add_or_remove_items'        => __('Add or remove ' . $titles, $text_domain),
    'choose_from_most_used'      => __('Choose from the most used', $text_domain),
    'popular_items'              => __('Popular ' . $Titles, $text_domain),
    'search_items'               => __('Search ' . $Titles, $text_domain),
    'not_found'                  => __('Not Found', $text_domain),
    'no_terms'                   => __('No ' . $Titles, $text_domain),
    'items_list'                 => __($Titles . ' list', $text_domain),
    'items_list_navigation'      => __($Titles . ' list navigation', $text_domain),
];
$args = [
    'label'         => __($Title, $text_domain),
    'description'   => __('Custom post type', $text_domain),
    'menu_position' => 4,
    'labels'        => $labels,
    'supports'      => ['title', 'thumbnail'],
    'taxonomies'    => ['post_tag', 'category'],
    'public'        => TRUE,
    'has_archive'   => TRUE,
    'rewrite'       => TRUE,
];

register_post_type($slug, $args);

add_rewrite_tag('%city%', '([^&]+)', 'city=');
add_rewrite_rule(
    '([^/]+)/([^/]+)/?$',
    'index.php?post_type=listings&city=$matches[1]&name=$matches[2]',
    'top'
);
1

There are 1 best solutions below

0
On

The only solution to this issue was to add a string to the beginning of my regex.

add_rewrite_rule(
    'listings/([^/]+)/([^/]+)/?$',
    'index.php?post_type=listings&city=$matches[1]&name=$matches[2]',
    'top'
);

Apparently, having a wildcard prefix in the add_rewrite_rule breaks Divi's Theme Builder. I'm guessing that it has something to do with how the Theme Builder handles rewrites in order to create specific templates. I'm going to have to create 301 redirects for all existing listings now, but the Theme Builder works.