<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.xsl" type="text/xsl"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>UGC &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/tag/ugc/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Fri, 06 Sep 2024 06:49:09 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>UGC &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[Automatically deleting WordPress comments using a theme]]></title>
		<link>https://shkspr.mobi/blog/2023/09/automatically-deleting-wordpress-comments-using-a-theme/</link>
					<comments>https://shkspr.mobi/blog/2023/09/automatically-deleting-wordpress-comments-using-a-theme/#respond</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Wed, 06 Sep 2023 11:34:47 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[UGC]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=46709</guid>

					<description><![CDATA[Let&#039;s say you want to automatically delete specific sorts of comments from your WordPress blog. For example, immediately trashing any comment that has a specific phrase &#34;Elephant Juice&#34; in it.  You can do this by going to Settings -&#62; Discussion → Disallowed Comment Keys. Or you can add something to your theme&#039;s functions.php file.  Here&#039;s the code snippet:  add_action( &#039;comment_post&#039;, &#039;…]]></description>
										<content:encoded><![CDATA[<p>Let's say you want to automatically delete specific sorts of comments from your WordPress blog. For example, immediately trashing any comment that has a specific phrase "Elephant Juice" in it.</p>

<p>You can do this by going to <a href="https://uk.godaddy.com/help/set-up-a-comment-blocklist-in-wordpress-26370">Settings -&gt; Discussion → Disallowed Comment Keys</a>. Or you can add something to your theme's <code>functions.php</code> file.</p>

<p>Here's the code snippet:</p>

<pre><code class="language-php">add_action( 'comment_post', 'check_comment_content', 10, 2 );

function check_comment_content( $comment_id, $comment_approved ) {
    $comment = get_comment( $comment_id );
    // Check if the comment body contains the text "Elephant Juice"
    if (stripos( $comment-&gt;comment_content, 'Elephant Juice') !== false ) {
        // Set the comment status to 'trash'
        wp_delete_comment( $comment_id );
    }
}
</code></pre>

<p>The <a href="https://developer.wordpress.org/reference/hooks/comment_post/"><code>comment_post</code> hook</a> is invoked immediately after a comment is received.</p>

<p>The comment is then retrieved by the function, checked for its content, and then deleted with <a href="https://developer.wordpress.org/reference/functions/wp_delete_comment/"><code>wp_delete_comment</code></a>.  If you're absolutely sure you don't want it in the trash (where it will stay for 30 days) you can call <code>wp_delete_comment( $comment_id, true );</code> and it will be instantly obliterated.</p>

<p>But it isn't <em>just</em> the comment's text that you can filter on. The comment object will look something like this (depending on what weird plugins you have installed):</p>

<pre><code class="language-php">[comment_ID] =&gt; 123456
[comment_post_ID] =&gt; 789
[comment_author] =&gt; Fred Flintstone
[comment_author_email] =&gt; fred@example.com
[comment_author_url] =&gt; https://example.com/@f_flintstone
[comment_author_IP] =&gt; 203.0.113.123
[comment_date] =&gt; 2023-08-29 07:13:57
[comment_date_gmt] =&gt; 2023-08-29 07:13:57
[comment_content] =&gt; Elephant Juice
[comment_karma] =&gt; 0
[comment_approved] =&gt; 0
[comment_agent] =&gt; Bridgy (https://brid.gy/about)
[comment_type] =&gt; like
[comment_parent] =&gt; 0
[user_id] =&gt; 0
...
</code></pre>

<p>So you can block comment by User Agent, IP address, and anything else you like. I use it to <a href="https://github.com/pfefferle/wordpress-webmention/issues/343">filter out specific WebMentions</a>.</p>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=46709&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2023/09/automatically-deleting-wordpress-comments-using-a-theme/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
