1

I am working with a multisite setup and I have created some custom post types, which seem to work fine except when trying to view any of the post types pages on the front end, I get a white screen. After looking into the issue it seems that flush rewrite rules fixes it. The problem with this is that admins wont have access to the permalinks setting so they cant flush it themselves.

I am currently using the following code to flush when activating a theme, but this is not ideal since a user will have to switch to another theme then switch back to the primary theme. So Im trying to find a way to modify the following code to work when a new site is made, thus having the custom post types working from the start. ---

/* Flush rewrite rules for custom post types. */
add_action( 'after_switch_theme', 'bt_flush_rewrite_rules' );

/* Flush your rewrite rules */
function bt_flush_rewrite_rules() {
     flush_rewrite_rules();
}

Any help is appreciated, thanks.

UPDATED

I have a main site that users sign up on the front end for a site like such mysite.com/theirsite. The default theme for them is set. Thats where Im trying to place the code so flush rewrite works when they create a site, not switch theme. Admin dashboard is simplified so they dont have access to permalinks or theme -> editor.

I would think though that when creating custom post types, it's suppose to flush rewrite, thus not even needing to use the extra code?

If it helps this is my register post type code --

http://pastebin.com/XsRjLj3q

1

You need to create a hook for wpmu_new_blog action:

add_action( 'wpmu_new_blog', 'wpse8170_wpmu_new_blog' );
function wpse8170_wpmu_new_blog( $blog_id ) {
    switch_to_blog( $blog_id );
    flush_rewrite_rules();
    restore_current_blog();
}
  • Initially I accepted your answer as the correct answer and upvoted but after doing some more testing it seems to not be working. when I save a post as draft and preview it seems to work, but when I publish the post I end up back at square one with a blank white page. Not sure why, being that the flush rewrite i use and posted in my question, works. – Rich Oct 2 '13 at 17:04
  • I think you need to call $wp_rewrite->init() after switch_to_blog(), otherwise you'll use the rewrite rules of the original blog with rebuilding the rules for the new blog. – Ian Dunn Nov 14 '14 at 20:17
0

are you deploying site yourself, or are you giving a files and letting others do it. are defining WP_DEFAULT_THEME in config to use a default theme, thereby negating after_switch_theme?

in my thinking, on a normal wp install, we upload files to server, define our db settings, hit the domain, wp displays a page for settings, click install, done. now you would normally change theme at this stage, but are you saying they will never select a theme as you have already defined one with WP_DEFAULT_THEME?

you could be naughty, add some code to front index.php, include a file that does your theme specific setup and once finished removes itself.

  • I updated my question – Rich Oct 2 '13 at 5:52
  • register_post_type() just registers a cpt with the wordpress system on each call, it has nothing to do with the db afaik. not knowing how you have setup the signup process, i will assume a form will be submitted with their options, will this be handled within wp context, if it is then this would be the place to call flush, be sure you have registered cpt's beforehand. – gwillie Oct 2 '13 at 6:14
0

I was experiencing a similar issue myself as part of automating a theme setup when creating a new blog on Multisite. After trying a variety of approaches I found an awesome article over at: https://jeremyfelt.com/2015/07/17/flushing-rewrite-rules-in-wordpress-multisite-for-fun-and-profit/#comment-141799

The article gives you plenty of different options, but in short the way I found to flush out the rewrite rules on my new site was to use:

/* Add action call to New Blog Hook */
add_action('wpmu_new_blog', 'setup_my_site', 10, 2);

// Setup function
function setup_my_site($blog_id, $user_id){

    // Switch blog context  
    switch_to_blog($blog_id);

    // Delete initial rule options
    delete_option( 'rewrite_rules' );

    // Restore original blog context
    restore_current_blog();

}

Hope that helps somebody in a similar situation :)

-1

Ok, I was able to figure it out. While Eugene Manuilov answer was good and may work for others, it unfortunately didnt work for me. What ended working for me was adding the flush rewrite right before the closing of my last custom post type.

function register_cpt_attorney() {

    $labels = array( 
        'name' => _x( 'Attorneys', 'attorney' ),
        'singular_name' => _x( 'Attorney', 'attorney' ),
        'add_new' => _x( 'Add New Attorneys', 'attorney' ),
        'add_new_item' => _x( 'Add New Attorney', 'attorney' ),
        'edit_item' => _x( 'Edit Attorney', 'attorney' ),
        'new_item' => _x( 'New Attorney', 'attorney' ),
        'view_item' => _x( 'View Attorney', 'attorney' ),
        'search_items' => _x( 'Search Attorneys', 'attorney' ),
        'not_found' => _x( 'No attorneys found', 'attorney' ),
        'not_found_in_trash' => _x( 'No attorneys found in Trash', 'attorney' ),
        'parent_item_colon' => _x( 'Parent Attorney:', 'attorney' ),
        'menu_name' => _x( 'Attorneys', 'attorney' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Your Attorney page(s).',
        'supports' => array( 'thumbnail' ),

        'public' => true,
        'show_ui' => true,
        'show_in_menu' => false,
        'menu_position' => 5,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'

    );
    register_post_type( 'attorney', $args );
 // add the following function if you are getting 
        // a 'Page not found' error from your permalink
        flush_rewrite_rules( false );
}

add_action( 'init', 'register_cpt_attorney' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.