php 从#comments中删除空锚#spam。 #WordPress

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 从#comments中删除空锚#spam。 #WordPress相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Removes comment spam anchors with empty nodeValues.
 *
 * @param $comment_content
 *
 * @return string
 */
function dmm_remove_empty_anchors_in_comments( $comment_content ) {
	// Grab the comment content.
	$dom_comment = new DOMDocument();
	$dom_comment->loadHTML( $comment_content );
	
	// Strip out the doctype and html wrapper.
	$dom_comment->removeChild( $dom_comment->doctype );
	$dom_comment->replaceChild($dom_comment->firstChild->firstChild->firstChild, $dom_comment->firstChild);

	// Get a NodeList of the anchors in the comment.
	$comment_anchors = $dom_comment->getElementsByTagName( 'a' );

	// We have to go through the nodeList backward to delete items. Oherwise we will only get every other empty link.
	for ( $i = $comment_anchors->length; --$i >=0; ) :
		$comment_anchor = $comment_anchors->item( $i );
		if ( trim ( $comment_anchor->nodeValue ) === '' ) :
			$comment_anchor->parentNode->removeChild( $comment_anchor );
		endif;
	endfor;

	// Save the comment without the spam links and send it back to be saved in the database.
	$comment_content = $dom_comment->saveHTML();

	return $comment_content;
}

// Grab the comment right before it is saved in the database.
add_filter( 'pre_comment_content', 'dmm_remove_empty_anchors_in_comments' );

以上是关于php 从#comments中删除空锚#spam。 #WordPress的主要内容,如果未能解决你的问题,请参考以下文章

如何删除 php 中的单行注释(例如“// 删除此注释”)?

如何使用 Perl 从文件中删除多行 C 注释?

使用jQuery在悬停时显示隐藏类

JPA/Hibernate - 删除子删除父(从同一个表)

JPA / Hibernate - 删除子项删除父项(从同一个表)

从Wordpress标题中删除';Private:';后缀