Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Trims text of spam comments on Admin→Comments→Spam down to a maximum of 42 characters. Will save you a heck of annoyance when you need to skim through spam comments. Because many of them tend to have looong paragraphs of text you don’t really wanna deal with; they are spam, after all.
<?php
/**
* Plugin Name: Trim Spam Comment Text
* Description: Trims text of spam comments on <a href="/wp-admin/edit-comments.php?comment_status=spam">Admin→Comments→Spam</a> down to a maximum of 42 characters. Will save you a heck of annoyance when you need to skim through spam comments. Because many of them tend to have looong paragraphs of text you don’t really wanna deal with; they are spam, after all.
* Version: 0.0.2
* Author: Caspar Hübinger
* Author URI: https://glueckpress.com/
* Plugin URI: https://gist.github.com/glueckpress/7316116faa491e09d199/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace Glck\Comments\Spam;
defined( 'ABSPATH' ) || exit;
add_filter( 'comment_text', __NAMESPACE__ . '\\comment_text', 10, 3 );
/**
* Hide comment text when in spam list table.
*
* @param string $comment_text Text of the current comment.
* @param object $comment The comment object.
* @param array $args Optional. An array of arguments. Default: empty array.
* @return string Filtered comment text
*/
function comment_text( $comment_text, $comment, $args = [] ) {
if ( ! is_admin() ) {
return $comment_text;
}
$screen = get_current_screen();
// edit-comments.php?comment_status=spam
if ( $screen->id === 'edit-comments'
&& isset( $_GET[ 'comment_status' ] ) // avoid PHP notice on edit-comments.php
&& $_GET[ 'comment_status' ] === 'spam' ) {
// Limit text to 42 characters maximum, so we can get but a glimpse.
$divider = strlen( utf8_decode( $comment_text ) ) < 42 ? 42 : strpos( $comment_text, ' ', 42 );
$trimmed = substr( $comment_text, 0, $divider );
$comment_text = sprintf( '%s [&#8230;]', $trimmed );
}
return $comment_text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment