How to get/track a list of subsites that were spawned from the current template

I want to get a list of subsites that were spawned from the current template - how could I get/track a subsite's source template?

Say I have two subsite templates, X & Y. Basically, I'm writing a plugin that needs to 1) know if it is inside a template site and 2) get a list of all other subsites that were built using the current template.
So, if run from inside the template X subsite, it would get a list of all other subsites that are also based upon template X. If the plugin were run from template Y, it would instead get a list of other sites that were based upon template Y.

My motivation is to build some tools to help propagate certain updates made to the original template off to its spawned subsites.

  • Ash
    • WordPress Hacker

    Hello @jjrohrer

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    I am tagging an available developer from second level support line in this thread for his valuable opinion on this issue. Please note that, developer response might be slower than usual staff response, so we appreciate your patience on this.

    Cheers
    Ash

  • Jose
    • Bruno Diaz

    Hi @jjrohrer,

    The idea sounds interesting. Still, you should be careful and take into account that the templates are not static but just a set of initial configurations. Therefore, if you force an update over all sites using template X, you might be overriding custom changes made to the template after the site was created.

    That said, I'm afraid that the plugin doesn't store the information that you are looking for.
    Your plugin will need to take care of storing the information when the template is assigned to the site.

    You can try using something like this:

    add_filter('blog_templates-blog_template', 'track_templates', 20, 3 );
    function track_templates($template, $blog_id, $user_id ){
         update_blog_option ($blog_id, 'nbt_original_template', $template);
    }

    Then, of course, you can easily get the original template for the current blog, and then loop through your subsites to find other blogs using the same template.

    Hope this helps. :slight_smile:

    Cheers,
    Jose

  • jjrohrer
    • Flash Drive

    So, in my initial testing, I have a little plugin, like so, the actually stores an array of ancestors-and-self ids. So far, so good:

    <?php
    /*
    	Plugin Name: et_blog_template_extensions
    	Plugin URI: xxx
    	Description: extend wpmudev's blog templates to track blog lineage
    	Version: 0.1
    	Author: JJ Rohrer
    	Author URI: http://www.ascendly.com/about/
        Depends: New Blog Templates
    */
    
    // Motivation: https://premium.wpmudev.org/forums/topic/how-to-gettrack-a-list-of-subsites-that-were-spawned-from-the-current-template#post-851991
    
    add_filter('blog_templates-blog_template', 'track_templates', 20, 3 );
    function append_ancestors($asrAncestorKeyId,$current_blog_id) {
        $asrAncestorKeyId[] = $current_blog_id;
        $ancestor_id = get_blog_option($current_blog_id,'nbt_ancestor_template_id',-1);
        if ($ancestor_id == -1 ) {
            return $asrAncestorKeyId;
        }
        return append_ancestors($asrAncestorKeyId,$ancestor_id);
    }
    
    function track_templates($template, $blog_id, $user_id ){
        $ancestor_blog_id = $template['blog_id'];
        $arrAncestorIds = append_ancestors([$blog_id],$ancestor_blog_id);
        update_blog_option ($blog_id, 'nbt_ancestor_template_ids', $arrAncestorIds);
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.