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 —
You need to create a hook for
wpmu_new_blog
action: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 negatingafter_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 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:
Hope that helps somebody in a similar situation 🙂
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.