Skip to content

Commit

Permalink
feat: support comments
Browse files Browse the repository at this point in the history
- adds comments template
- remove and disable pingbacks and trackbacks on posts
- adds documentation to explain how to turn comments off/on
  • Loading branch information
rise-erpelding committed Oct 24, 2023
1 parent 99c1246 commit 0f71a0c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ Most likely, you want your urls to be more user/SEO friendly, like `example.com/

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

### Comments

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.

#### Turn off Comments on a Single Post

1. From the admin dashboard, navigate to the "Edit Post" page
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)
1. Scroll down the menu to the option with the heading "Discussion", uncheck "Allow comments" and update the post

#### Turn off Comments on Future Posts

This will uncheck "Allow comments" on individual posts going forward by default, but comments can be enabled for select posts.

1. From the admin dashboard, click on "Discussion" under the "Settings" menu on the left side
1. Under "Default post settings", uncheck "Allow people to submit comments on new posts"
1. Scroll to the bottom and hit "Save Changes"

#### Comments for Pages
By default, comments are turned off for pages, but can be enabled for pages individually:

1. From the admin dashboard, navigate to "Edit Page"
1. Access the Page settings menu on the right side of the page
1. Scroll down the menu to the option with the heading "Disucssion", check "Allow comments", and update the page

### Template Hierarchy

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/).
Expand Down
15 changes: 15 additions & 0 deletions src/php/comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* The template for displaying comments.
*
* Loaded by comments_template().
*
* @link https://developer.wordpress.org/reference/functions/comments_template/
*/

// https://timber.github.io/docs/reference/timber/#context
$context = Timber\Timber::context();

$context['should_show_avatars'] = get_option( 'show_avatars' );

Timber\Timber::render( 'partials/comments.twig', $context );
30 changes: 30 additions & 0 deletions src/php/inc/theme-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,33 @@ function custom_hide_editor() {
}
}
add_action( 'admin_init', 'custom_hide_editor' );

/**
* Customize the pingbacks and trackbacks option on new posts by default.
*
* @param mixed $value The current option value.
* @param string $option The name of the option being filtered.
* @param mixed $default The default value for the option.
*
* @return mixed The filtered option value. If the provided option is 'default_ping_status',
* this function will set its value to 'closed' (disabling pingbacks and trackbacks),
* and return the updated value. For other options, it returns the original value.
*/
function custom_disable_pingbacks_trackbacks_option( $value, $option, $default ) {
if ( 'default_ping_status' === $option ) {
$value = 'closed';
}

return $value;
}

add_filter( 'pre_option_default_ping_status', 'custom_disable_pingbacks_trackbacks_option', 10, 3 );

/**
* Remove Trackbacks Support for the "post" Post Type.
*/
function remove_trackbacks_support() {
remove_post_type_support( 'post', 'trackbacks' );
}

add_action( 'init', 'remove_trackbacks_support' );
3 changes: 2 additions & 1 deletion src/php/views/page.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "layouts/base.twig" %}

{% block content %}

<div class="obj-width-limiter">
<img
src="{{ post.thumbnail.src|resize(960) }}"
Expand All @@ -10,6 +9,8 @@
{% endif %}
/>
{{ post.content }}
{# Loads comments.php by default #}
{{ function('comments_template') }}
</div>

{% endblock %}
18 changes: 18 additions & 0 deletions src/php/views/partials/comments.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% if function('have_comments') %}
<div id="comments" class="{{ html_classes('post-comments', {
'show-avatars': should_show_avatars
}) }}">
<h2>Comments</h2>
<ul>
{{ function('wp_list_comments') }}
</ul>
{{ function('comment_form') }}
</div>
{% if not function('comments_open') %}
<p>Comments are closed</p>
{% endif %}
{% else %}
{% if function('comments_open') %}
{{ function('comment_form') }}
{% endif %}
{% endif %}

0 comments on commit 0f71a0c

Please sign in to comment.