Skip to content

Commit 0f71a0c

Browse files
feat: support comments
- adds comments template - remove and disable pingbacks and trackbacks on posts - adds documentation to explain how to turn comments off/on
1 parent 99c1246 commit 0f71a0c

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,31 @@ Most likely, you want your urls to be more user/SEO friendly, like `example.com/
186186

187187
For other options see the [WordPress docs](https://wordpress.org/documentation/article/customize-permalinks/) for more info.
188188

189+
### Comments
190+
191+
Comments for posts are supported out of the box, but can be turned off for single posts or for any new posts going forward. Comments can be approved, edited, or trashed from the "Comments" menu from the admin dashboard.
192+
193+
#### Turn off Comments on a Single Post
194+
195+
1. From the admin dashboard, navigate to the "Edit Post" page
196+
1. Access the Post settings menu on the right side of the page, if it is not already open (the settings menu can be opened with the button to the right of the "Update" button)
197+
1. Scroll down the menu to the option with the heading "Discussion", uncheck "Allow comments" and update the post
198+
199+
#### Turn off Comments on Future Posts
200+
201+
This will uncheck "Allow comments" on individual posts going forward by default, but comments can be enabled for select posts.
202+
203+
1. From the admin dashboard, click on "Discussion" under the "Settings" menu on the left side
204+
1. Under "Default post settings", uncheck "Allow people to submit comments on new posts"
205+
1. Scroll to the bottom and hit "Save Changes"
206+
207+
#### Comments for Pages
208+
By default, comments are turned off for pages, but can be enabled for pages individually:
209+
210+
1. From the admin dashboard, navigate to "Edit Page"
211+
1. Access the Page settings menu on the right side of the page
212+
1. Scroll down the menu to the option with the heading "Disucssion", check "Allow comments", and update the page
213+
189214
### Template Hierarchy
190215

191216
This starter template covers the generic templates needed for things like single post pages, archive (or listing) pages, the 404 page, and the search page, but you can override those using [WordPress' template hierarchy](https://developer.wordpress.org/themes/basics/template-hierarchy/).

src/php/comments.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* The template for displaying comments.
4+
*
5+
* Loaded by comments_template().
6+
*
7+
* @link https://developer.wordpress.org/reference/functions/comments_template/
8+
*/
9+
10+
// https://timber.github.io/docs/reference/timber/#context
11+
$context = Timber\Timber::context();
12+
13+
$context['should_show_avatars'] = get_option( 'show_avatars' );
14+
15+
Timber\Timber::render( 'partials/comments.twig', $context );

src/php/inc/theme-setup.php

+30
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,33 @@ function custom_hide_editor() {
9595
}
9696
}
9797
add_action( 'admin_init', 'custom_hide_editor' );
98+
99+
/**
100+
* Customize the pingbacks and trackbacks option on new posts by default.
101+
*
102+
* @param mixed $value The current option value.
103+
* @param string $option The name of the option being filtered.
104+
* @param mixed $default The default value for the option.
105+
*
106+
* @return mixed The filtered option value. If the provided option is 'default_ping_status',
107+
* this function will set its value to 'closed' (disabling pingbacks and trackbacks),
108+
* and return the updated value. For other options, it returns the original value.
109+
*/
110+
function custom_disable_pingbacks_trackbacks_option( $value, $option, $default ) {
111+
if ( 'default_ping_status' === $option ) {
112+
$value = 'closed';
113+
}
114+
115+
return $value;
116+
}
117+
118+
add_filter( 'pre_option_default_ping_status', 'custom_disable_pingbacks_trackbacks_option', 10, 3 );
119+
120+
/**
121+
* Remove Trackbacks Support for the "post" Post Type.
122+
*/
123+
function remove_trackbacks_support() {
124+
remove_post_type_support( 'post', 'trackbacks' );
125+
}
126+
127+
add_action( 'init', 'remove_trackbacks_support' );

src/php/views/page.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{% extends "layouts/base.twig" %}
22

33
{% block content %}
4-
54
<div class="obj-width-limiter">
65
<img
76
src="{{ post.thumbnail.src|resize(960) }}"
@@ -10,6 +9,8 @@
109
{% endif %}
1110
/>
1211
{{ post.content }}
12+
{# Loads comments.php by default #}
13+
{{ function('comments_template') }}
1314
</div>
1415

1516
{% endblock %}

src/php/views/partials/comments.twig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% if function('have_comments') %}
2+
<div id="comments" class="{{ html_classes('post-comments', {
3+
'show-avatars': should_show_avatars
4+
}) }}">
5+
<h2>Comments</h2>
6+
<ul>
7+
{{ function('wp_list_comments') }}
8+
</ul>
9+
{{ function('comment_form') }}
10+
</div>
11+
{% if not function('comments_open') %}
12+
<p>Comments are closed</p>
13+
{% endif %}
14+
{% else %}
15+
{% if function('comments_open') %}
16+
{{ function('comment_form') }}
17+
{% endif %}
18+
{% endif %}

0 commit comments

Comments
 (0)