Skip to content

Instantly share code, notes, and snippets.

@philipjohn
Created October 1, 2019 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipjohn/609add03aa8f9b4b12a72db22de43280 to your computer and use it in GitHub Desktop.
Save philipjohn/609add03aa8f9b4b12a72db22de43280 to your computer and use it in GitHub Desktop.
Rename Comments PoC
<?php
/**
* Plugin Name: Rename Comments
* Description: Provides themes with the ability to allow users to rename comments.
* Version: 0.1.0
* Author: Philip John, Automattic
* Author URI: https://automattic.com
* Text Domain: rename-comments
* Domain Path: /languages
*/
namespace Rename_Comments;
function admin_init() {
add_settings_section(
'rename_comments',
esc_html__( 'Rename Comments', 'rename-comments' ),
__NAMESPACE__ . '\settings_section_output',
'discussion'
);
// Comment name
add_settings_field(
'comments_name',
esc_html__( 'Name', 'rename-comments' ),
__NAMESPACE__ . '\field_comments_name',
'discussion',
'rename_comments'
);
register_setting(
'discussion',
'rc_comments_name',
[
'type' => 'string',
'sanitise_callback' => 'esc_html',
]
);
// Comment name (plural)
add_settings_field(
'comments_name_plural',
esc_html__( 'Name (plural)', 'rename-comments' ),
__NAMESPACE__ . '\field_comments_name_plural',
'discussion',
'rename_comments'
);
register_setting(
'discussion',
'rc_comments_name_plural',
[
'type' => 'string',
'sanitise_callback' => 'esc_html',
]
);
// "Leave a comment"
add_settings_field(
'comments_leave_comment',
esc_html__( 'Leave a comment', 'rename-comments' ),
__NAMESPACE__ . '\field_comments_leave_comment',
'discussion',
'rename_comments'
);
register_setting(
'discussion',
'rc_comments_leave_comment',
[
'type' => 'string',
'sanitise_callback' => 'esc_html',
]
);
// "Comments are closed"
add_settings_field(
'comments_comments_closed',
esc_html__( 'Comments are closed', 'rename-comments' ),
__NAMESPACE__ . '\field_comments_comments_closed',
'discussion',
'rename_comments'
);
register_setting(
'discussion',
'rc_comments_comments_closed',
[
'type' => 'string',
'sanitise_callback' => 'esc_html',
]
);
// Heading (no comments)
add_settings_field(
'no_comments_title',
esc_html__( 'Heading (no comments)', 'rename-comments' ),
__NAMESPACE__ . '\field_no_comments_title',
'discussion',
'rename_comments'
);
register_setting(
'discussion',
'rc_no_comments_title',
[
'type' => 'string',
'sanitise_callback' => 'esc_html',
]
);
// Heading (with comments)
add_settings_field(
'comments_title',
esc_html__( 'Heading (with comments)', 'rename-comments' ),
__NAMESPACE__ . '\field_comments_title',
'discussion',
'rename_comments'
);
register_setting(
'discussion',
'rc_comments_title',
[
'type' => 'string',
'sanitise_callback' => 'esc_html',
]
);
}
add_action( 'admin_init', __NAMESPACE__ . '\admin_init' );
/**
* Removes the meta from the comment text on the front end.
*
* It's added by the WP Comment Meta plugin we're using, but we don't want it on the front.
* @return void
*/
function remove_meta_from_comment() {
global $nmwpcomment;
remove_filter( 'comment_text', array( $nmwpcomment, 'render_comment_meta_front' ), 100 );
}
add_action( 'init', __NAMESPACE__ . '\remove_meta_from_comment' );
function settings_section_output() {
echo esc_html__( 'Provide your own text to rename the comments section as you desire.', 'rename-comments' );
}
function field_comments_name() {
$value = get_option( 'rc_comments_name' );
?>
<p><label for="comments_name"><?php esc_html_e( 'This is the new name for a "Comment". E.g. you might want to call them "Letters".', 'rename-comments' ); ?></label></p>
<p><input id="comments_name" name="rc_comments_name" value="<?php echo esc_attr( $value ) ?>" /></p>
<?php
}
function field_comments_name_plural() {
$value = get_option( 'rc_comments_name_plural' );
?>
<p><label for="comments_name_plural"><?php esc_html_e( 'This is the new name for a "Comment" in it\'s plural form.', 'rename-comments' ); ?></label></p>
<p><input id="comments_name_plural" name="rc_comments_name_plural" value="<?php echo esc_attr( $value ) ?>" /></p>
<?php
}
function field_comments_leave_comment() {
$value = get_option( 'rc_comments_leave_comment' );
?>
<p><label for="comments_leave_comment"><?php esc_html_e( 'This text will replace the "Leave a comment" message.', 'rename-comments' ); ?></label></p>
<p><input id="comments_leave_comment" name="rc_comments_leave_comment" value="<?php echo esc_attr( $value ) ?>" /></p>
<?php
}
function field_comments_comments_closed() {
$value = get_option( 'rc_comments_comments_closed' );
?>
<p><label for="comments_comments_closed"><?php esc_html_e( 'This text will replace the "Comments are closed" message.', 'rename-comments' ); ?></label></p>
<p><input id="comments_comments_closed" name="rc_comments_comments_closed" value="<?php echo esc_attr( $value ) ?>" /></p>
<?php
}
function field_no_comments_title() {
$value = get_option( 'rc_no_comments_title' );
?>
<p><label for="no_comments_title"><?php esc_html_e( 'This will be the heading for the section when there are no comments yet.', 'rename-comments' ); ?></label></p>
<p><input id="no_comments_title" name="rc_no_comments_title" value="<?php echo esc_attr( $value ) ?>" /></p>
<?php
}
function field_comments_title() {
$value = get_option( 'rc_comments_title' );
?>
<p><label for="comments_title"><?php esc_html_e( 'This will be the heading for the section when there are existing comments.', 'rename-comments' ); ?></label></p>
<p><input id="comments_title" name="rc_comments_title" value="<?php echo esc_attr( $value ) ?>" /></p>
<?php
}
function get_text( $id ) {
$value = get_option( $id );
if ( $value ) {
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment