Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#56939 closed defect (bug) (worksforme)

Twenty Twenty-Three never calls comments_template

Reported by: justin_k's profile Justin_K Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.1
Component: Bundled Theme Keywords: close
Focuses: Cc:

Description (last modified by mukesh27)

All themes are supposed to apply the comments_template filter to allow plugins to replace the comment template. Twenty Twenty-Three does not.

Repro:

// Pages still load fine with this here, which proves that it is never calling this filter as it should
add_filter('comments_template', 'suppress_comments');
function suppress_comments($file)
{
    die("it was called");
}

Twenty Twenty-Three thus breaks any plugins that rely on this filter to swap out the comment template.

Attachments (1)

Capture d’écran 2022-11-01 à 10.20.44.2.png (193.8 KB) - added by audrasjb 2 years ago.
Site editor -> Template parts -> Comments template

Download all attachments as: .zip

Change History (15)

#1 @Justin_K
2 years ago

Looks like the description doesn't work with standard markdown, so my code block looks funky...and it seems like this doesn't allow editing of our own tickets...?

#2 @mukesh27
2 years ago

  • Component changed from General to Bundled Theme
  • Description modified (diff)
  • Version set to 6.1

@audrasjb
2 years ago

Site editor -> Template parts -> Comments template

#3 @audrasjb
2 years ago

  • Keywords close added

Hello @Justin_K

This is an expected change, since Twenty Twenty-Three doesn't work with PHP template files. The comment template part is directly generated from the Site Editor, in the admin.

Comment Template can be edited here:
Go to Appearance > Editor > WordPress logo button at the top left > Template Parts > Comments.
(see screenshot above)

#4 @Justin_K
2 years ago

That's how a user can manually edit it - how does a plugin programmatically edit/remove it on a specific page id? i.e. the following is now broken/no effect, what's the replacement?:

<?php
add_filter('comments_template', 'suppress_comments');
function suppress_comments($file)
{
    global $post, $opt_vgb_page;
    if ($post->ID == get_option($opt_vgb_page)) return dirname(__FILE__) . '/_blank.php';
    else                                         return $file;
}

#5 follow-up: @audrasjb
2 years ago

There is no replacement since there's no related PHP code in the theme files.
With block themes, this template part appears to be managed on the Site Editor and nowhere else.

#6 @Justin_K
2 years ago

So...it just simply breaks all plugins that rely on this? No thought given to how a plugin can hide comments on a particular page, as has been possible in WordPress since at least 2010?

If themes are moving away from PHP, that's fine / makes sense. But seems very odd to just break something & not provide any replacement. Plugins still need a way to hook into showing/hiding templates, or they will simply be rendered unusable.

#7 @audrasjb
2 years ago

I don't know your specific use case, but you can of course interact with the Site Editor directly. It really depends on your needs so I can't give you the correct way to do it without knowing your use case.

But indeed, you shouldn't only rely on comments_template since it won't work on block themes: their template part are managed in the site editor.

Last edited 2 years ago by audrasjb (previous) (diff)

#8 @Justin_K
2 years ago

Use case is to hide/remove the comments output from a specific pageID - i.e. pretty much the precise codeblock from above:

add_filter('comments_template', 'suppress_comments');
function suppress_comments($file)
{
    global $post, $opt_vgb_page;
    if ($post->ID == get_option($opt_vgb_page)) return dirname(__FILE__) . '/_blank.php';
    else                                         return $file;
}

(where _blank.php is empty)

#9 @TimothyBlynJacobs
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

If you are looking to hide comments for a specific page, I'd recommend closing comments for that post via the Block Editor.

If you need to do it programatically, you can use the comments_open filter instead.

<?php
add_filter( 'comments_open', function ( $open, $post_id ) {
        if ( $post_id === 5 ) {
                $open = false;
        }

        return $open;
}, 10, 2 );

5 in this case is a placeholder for whichever post ID you'd like to disable comments for.

This trac is used for managing bugs and enhancements affecting WordPress Core. For further help with developing your site, I'd recommend visiting the Support Forums.

#10 @Justin_K
2 years ago

That doesn't actually replicate the above - it just switches off the ability to submit new comments. The previous code hides comments entirely (both rendering comments, and rendering the submission form - that suggestion only does the latter).

The reason I reported this as a bug is because the comments_template filter no longer functions properly, and so far there does not seem to be a proper replacement. As the plugin is not under active development, & has remained untouched & functional for over a decade, this really isn't a request for "help developing my plugin." Typically when platforms make breaking changes, it is mentioned in the documentation. But neither the documentation for the comments_template filter, nor the WP 6.1 update notification, provided notes regarding this breaking change.

Once an actual replacement is identified, it should probably go in the documentation for that filter. But thus far, I still see no suitable replacement for the now-broken comments_template filter.

#11 @TimothyBlynJacobs
2 years ago

If you also want to hide existing comments, you can use the pre_render_block to shortcircuit the core/comments block and return an empty string.

Last edited 2 years ago by TimothyBlynJacobs (previous) (diff)

#12 @Justin_K
2 years ago

There we go, that's the solution. So the functionally identical replacement for:

add_filter('comments_template', 'suppress_comments');
function suppress_comments($file)
{
    global $post, $opt_vgb_page;
    if ($post->ID == get_option($opt_vgb_page)) return dirname(__FILE__) . '/_blank.php';
    else                                         return $file;
}

seems to be:

add_filter('pre_render_block', function($pre_render, $parsed_block){
    global $post, $opt_vgb_page;
    if($post->ID == get_option($opt_vgb_page) && $parsed_block['blockName'] == "core/comments"){
        return "";
    }
}, 10, 2);

Will also post a comment on the comments_template hook page. Thanks!

#13 @audrasjb
2 years ago

Will also post a comment on the comments_template hook page

Thank you! I'll make sure it's validated asap :)

#14 in reply to: ↑ 5 @Justin_K
2 years ago

Replying to audrasjb:

There is no replacement since there's no related PHP code in the theme files.
With block themes, this template part appears to be managed on the Site Editor and nowhere else.

Actually, isn't Twenty Twenty-Two also a block theme? Because the comments_template does still work properly there.

Note: See TracTickets for help on using tickets.