Open mithat opened 2 months ago
I have begun to implement a way to disable comments per post using the theme rather than do a core hack.
The approach is similar to how a table of contents is implemented, but rather than add:
<!--toc-->
to a post on which you want a table of contents, you add:
<!--no-comments-->
to posts you don't want to have comments.
You then need to modify the appropriate code in your theme. In twenty-sixteen's post.html.php
, I changed:
<?php if (disqus()): ?>
<?php echo disqus($p->title, $p->url) ?>
<?php endif; ?>
<?php if (disqus_count()): ?>
<?php echo disqus_count() ?>
<?php endif; ?>
<?php if (facebook() || disqus()): ?>
<div class="comments-area" id="comments">
<h2 class="comments-title"><?php echo i18n('Comments');?> “<?php echo $p->title;?>”</h2>
<?php if (facebook()): ?>
<div class="fb-comments" data-href="<?php echo $p->url ?>" data-numposts="<?php echo config('fb.num') ?>" data-colorscheme="<?php echo config('fb.color') ?>"></div>
<?php endif; ?>
<?php if (disqus()): ?>
<div id="disqus_thread"></div>
<?php endif; ?>
</div>
<?php endif; ?>
to:
<?php $no_comments = explode('<!--no-comments-->', $post->body); ?>
<?php if (!isset($no_comments['1'])): ?>
<?php if (disqus()): ?>
<?php echo disqus($p->title, $p->url) ?>
<?php endif; ?>
<?php if (disqus_count()): ?>
<?php echo disqus_count() ?>
<?php endif; ?>
<?php if (facebook() || disqus()): ?>
<div class="comments-area" id="comments">
<h2 class="comments-title"><?php echo i18n('Comments');?> “<?php echo $p->title;?>”</h2>
<?php if (facebook()): ?>
<div class="fb-comments" data-href="<?php echo $p->url ?>" data-numposts="<?php echo config('fb.num') ?>" data-colorscheme="<?php echo config('fb.color') ?>"></div>
<?php endif; ?>
<?php if (disqus()): ?>
<div id="disqus_thread"></div>
<?php endif; ?>
</div>
<?php endif; ?>
<?php endif; ?>
Basically, I wrapped the comment insertion code between:
<?php $no_comments = explode('<!--no-comments-->', $post->body); ?>
<?php if (!isset($no_comments['1'])): ?>
and
<?php endif; ?>
So far, this seems to be working fine.
I have yet to address how I want to handle comment counts in the post info and whether I want to add a "Comments have been disabled for this post" notification where comments would normally go. But overall this seems to be a workable approach until (if ever!) the feature is added to the core.
I'm a bit late to propose some kind of a workaround, but was doing other things with htmly, as you could see in other "issues".
Basically htmly lacks adding some sort of custom fields for content management in admin panel, although such functionality is available for templates. But didn't you consider to base on tags rather than loading whole page's content to check whether it contains a string ? Checking if page's tags contain a #nocomments using get_tag() funct you could either display comments or load 3rd party script, or show a custom notification. Assuming you don't enter a list of tags longer than page content 😅 Of course displaying tags in content would have to be changed not to display certain tags you'd use for functional behavior, but you get the point.
Would be great if @danpros enabled discussions here, or implemented a simple forum on htmly's website to discuss things related to htmly rather than spamming issues section.
@Joduai I have other personal projects that really needs attention so I've been pretty inactive here lately. Already enable the discussion.
I liked the <!--no-comments->
tag idea so I wrote up something quick to add that to the markdown file and added it to the add/edit post pages in the admin area, does something like this work per post or should I change it to a checkbox or other input type?
@KuJoe Since this is something that impacts the content text, I'm wondering whether it might be better to add a "No comments" icon to the text editor toolbar instead. A dropdown or checkbox is nice, but it could lead to the following scenario: User disables comments, which adds the <!--no-comments-->
tag to the content. User then later enables comments using the UI -- but the <!--no-comments-->
isn't removed from the content (unless you also want to add the code needed to parse the content and remove it).
Putting it in the toolbar makes it behave like the TOC button, which while not ideal is arguably more consistent.
The bigger question is whether the <!--no-comments-->
hack should creep toward permanence with any UI enhancements at all. The effort to do it IMHO the right way, with an additional state variable attached to the post, probably isn't that much greater than the hack offered here. I just did it this way so I didn't have to modify the core -- at least until I have a better understanding of how HTMLy works.
@KuJoe Since this is something that impacts the content text, I'm wondering whether it might be better to add a "No comments" icon to the text editor toolbar instead. A dropdown or checkbox is nice, but it could lead to the following scenario: User disables comments, which adds the
<!--no-comments-->
tag to the content. User then later enables comments using the UI -- but the<!--no-comments-->
isn't removed from the content (unless you also want to add the code needed to parse the content and remove it).
The implementation I coded does remove it when it's enabled so that shouldn't be an issue. I considered adding it to the toolbar but since this is more a setting than a content addition I felt it would be better added to the rest of the post settings rather than the body of the post. I'm happy to change it though if everybody likes the toolbar method better.
EDIT: And just to expand on this a bit so you better understand how the add/edit post functions work, when you "edit" an existing post it actually recreates the whole thing each time so it's not actually editing the content file. In this case every time the file is saved it checks if comments are enabled or disabled and either includes or excludes the tag depending on the setting when the submit button is pressed so it's a much easier task than parsing existing text and trying to remove it. If it wasn't so simple then your method would 100% be the easiest and safest implementation and I wouldn't have bothered going this route. 😉
Comments may not be desired on all blog posts.
Assuming you have enabled comments in HTMLy, it would be great if you could disable them for some posts. Disqus (and possibly other comment providers) lets you close comments per-post, but visible code injected by Disqus remains.
Many blogging platforms have a simple checkbox on a post's editing page that lets you disable comments for that post.