Search code examples
wordpressthemesdivi

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


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

Solution

  • Found another solution using ChatGPT...

    I added a method for checking if the Visual Builder or Theme Builder is in use:

    function is_divi_builder_active() {
        // Check if the Divi Visual Builder is active
        if (isset($_GET['et_fb']) && $_GET['et_fb'] === '1') {
            return true;
        }
    
        // Check for Divi Theme Builder
        if (isset($_GET['et_theme_builder']) && $_GET['et_theme_builder'] === '1') {
            return true;
        }
    
        // Additional checks for Divi Theme Builder can be added here
        // Example: Check for specific Divi Builder global variables
        if (defined('ET_BUILDER_THEME') && ET_BUILDER_THEME) {
            return true;
        }
    
        return false;
    }
    

    Then I updated my add_rewrite_rule method to check:

    public static function add_rewrite_rules() {
        if(!self::is_divi_builder_active()){
            add_rewrite_rule(
                '^([^/]+)/([^/]+)/?$',
                'index.php?post_type=listings&name=$matches[2]',
                'top'
            );
        }
    }
    

    Worked like a charm! My rewrite rule works and I can edit with Visual Builder and Theme Builder without errors.