Comments not copied with New Blog Templates

I'm running WP 3.9.1 and New Blog Templates 2.6.4.

I've set up a template site which is basically the default blank site you get with WP but with a few additional settings.

When I use this as a template for a new blog the default comment from 'Anonymous' is not copied to the new site.

I have the template site set to copy everything except users (although changing it to copy users makes no difference).

If I go to the post listing in the dashboard of the new site, the 'Hello World' post shows there is one comment, but when you click on the speech bubble to take you to the comments page it says "No comments found."

Having a quick look at the code line 59 of blogtemplatesfiles/copier.php does not tie up with the comment preceding it.

Thanks,

Tom

  • Jack Kitterhing

    Hi there Tom,

    Hope you're well today and thanks for your question.

    2.6.4 is quite a few versions behind, do you have any custom code included within the new blog templates plugin files?

    If you don't, can you upgrade to 2.6.8.2 and see if that works correctly for you, they do include multiple fixes for various issues including terms and taxonomies. :slight_smile:

    Thank you!

    Kind Regards
    Jack.

  • SHU

    My test server is not available at the moment so I can't try 2.6.8.2 but looking at the code in 2.6.8.2 the same issue persists. This is also shown by setting the template blog to not copy post nor pages, in this case the posts, pages and comments are still copied.

    Both this issue and the one above are linked to the incorrect logic in line 59 of copier.php. If the code is changed from:

    if ( $this->settings['to_copy']['posts'] || $this->settings['to_copy']['pages'] ) {

    to:

    if ( ! ($this->settings['to_copy']['posts'] && $this->settings['to_copy']['pages']) ) {

    that should fix the problem (although until my test server is back I can't test this).

    Thanks,

    Tom

  • Jose

    Hey @SHU,

    Hope you are doing great today!

    You are right about the speech bubble showing the comment count number. That's definitively a bug.

    Regarding the comments not being copied, that's actually the expected behavior. Unfortunately, the plugin doesn't provide that feature.
    The plugin will keep the comments if the destination site DB already contains comments (quite odd since it is supposed to be a new site), BUT it will erase all the comments if the template includes post and pages. That's what the line 59 mentioned above is doing.

    In order to fix the post comment count issue, you can implement the following hook:

    add_filter( 'blog_templates-process_row', 'process_row', 10, 3);
    
          function process_row($row, $dest_table, $templated_blog_id) {
                if($dest_table === 'posts'){
                    $row['comment_count'] = 0;
                }
                return $row;
            }

    This will reset the comment count for post and pages to 0.

    Now, if you need to actually copy the comments from the "templated" site, then it will require a good amount of customization.

    The comments and commentmeta tables are set as default tables, and therefore you can't select those as "additional tables" when you create the template.
    I made the following code that will force the comment tables as additional tables and remove it from the default tables.
    This still needs some testing and maybe some modifications, but it would be the right approach:

    add_filter( 'nbt_copy_additional_tables', 'set_custom_additional_tables', 10, 1);
    add_filter( 'blog_templates-blog_template', 'set_blog_template', 10, 3);
    
    function set_blog_template($template, $blog_id, $user_id ){
                global $wpdb;
                $this->blog_template_id = $template['blog_id'];
                $pfx = $wpdb->get_blog_prefix( $this->blog_template_id );
                $this->additional_tables = array($pfx.'comments', $pfx.'commentmeta');
                $template['additional_tables'] = array_unique (array_merge ($template['additional_tables'], $this->additional_tables));
                return $template;
            }
    
    function set_custom_additional_tables( $all_source_tables ){
                $new_source_tables = array_unique (array_merge ($all_source_tables, $this->additional_tables));
                return $new_source_tables;
            }

    Please let me know if this works for you.

    Thanks,
    JJ

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.