I'm developing a Multisute plugin that is supposed to setup sites using a specific template, including setting up several static pages and menu items, as well as a "Blog" category for blog posts.
The category is created fine, and has one post in it, and I've figured out how to setup the menu link to the blog category using the
page_for_posts option. However, WordPress creates default links to each category in the sidebar of the blog under "Categories". And the link it creates there is broken.
It links to
/category/blog, but when you click on the link, you get a 404 - "Well, this is embarassing...". The same link shows up in the footer of the default 404 template, and is still broken (obviously).
I don't know why this link doesn't work, but I've figured out that if I visit the permalink settings page (or simply run
flush_rewrite_rules();) from the created site, it works. So I've run that from the plugin's setup script, using switch_to_blog, but for some reason, since the plugin script runs on the main site, it doesn't work. I still get a 404 for that URL until after I run it from the created site (or visit the permalink structures page on the created site). Here's the debug script I've created that works if I visit it from the created site, but not from the main site:
//Filename: wp-admin/debug.php require_once('./admin.php'); global $wp_rewrite; switch_to_blog(84); //this is the created blog in question $wp_rewrite->init(); //JUST to make sure it's initialized for the correct blog $wp_rewrite->flush_rules(); restore_current_blog(); $wp_rewrite->init(); //To make sure it's set back to where it should be.
So apparently there's a difference between running it on the main site, and on the created site. I don't know how rewrite rules work with wordpress multisite... so I don't know how to find what is changing in one case but not in another. The rules returned from
get_option(rewrite_rules) for blog 84 are almost identical in either case, so I feel like there must be another step happening to refresh some rewrite cache or something when I run it from the created site (blog 84), that is NOT happening when I run it from the main site.
Any ideas? Does inserting a custom category on a newly created site work for you? How can I find out what is changing to make it work when I run flush_rewrite_rules from the created site?