0

TL:DR; Is it possible to restrict page templates to a certain site within a multisite network? (Using something like get_current_blog_id for e.g)

More details

I’m building out several websites that each use the same base partials, just in different variations in their page templates. For e.g Site A & Site B’s home page both use Components A - D, however Site A’s goes A B C D and Site B’s goes C B A D. All my actual markup goes into the component partials and the page templates are mostly just a list of includes (using Roots Sage theme to help pass data).

This is the basic premise but we’re talking +-30 components and +-50 templates across 7 websites.

I’ve got all my templates in a subdirectory in my theme folder (e.g template-home-site-a, template-home-site-b) however this means when a user is working in the CMS for Site A they get a list of 50 templates, rather than just ones specific to Site A which might only be around 12.

Is there a way to possibly hook into Wordpress’ templating system and add/remove folders it looks in based on the current blog ID? Open to any other suggestions.

4
  • 3
    Hi Mikey! Welcome to the forum. While I could probably come up with a method to do what you are asking, your users would suffer very poor performance. Even caching could be a challenge. It would be far better to create child themes for each of your sites, with only the resources each requires. This reduce the number of calculations for drawing each page, resulting in faster load times. This also puts you ahead of the usual tweaks each site will require, while improving SEO. Good luck! Nov 12, 2019 at 17:36
  • Hi @MikeBaxter Thanks for getting back to me. That sounds like it could be a good alternative, I’ll give it a bash tomorrow and see if I can get it working 100%. Cheers.
    – Mikey
    Nov 12, 2019 at 17:44
  • Child themes are a superior solution. Always ask about the challenge you're facing not how to implement a solution to it.
    – Tom J Nowell
    Nov 12, 2019 at 18:25
  • 1
    Also, if you had discrete components, why not make them blocks? Then you can rearrange them in the block editor on the page assigned to the homepage, no child themes or custom template folders or template files needed at all
    – Tom J Nowell
    Nov 12, 2019 at 18:26

1 Answer 1

1

I know it is a bit old question. But I hope some one can find it usefull. If for some reason you can't use child-theme (like me) there is hook theme_page_templates Accordint to developer WP Page Templates. Example:

function makewp_exclude_page_templates( $post_templates ) {
  if ( is_multisite() && !is_main_site() ) {
    unset( $post_templates['wp-templates/iframe_container.php'] );
    //In my case this one is removed from the list on every page other than main site
  } 
  return $post_templates;
}
add_filter( 'theme_page_templates', 'makewp_exclude_page_templates' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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