<?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>
	<copyright>© Terence Eden. 🄯 CC BY-SA. See https://shkspr.mobi/blog/copyright-and-copyleft/</copyright>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.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[Terence Eden]]></dc:creator>
		<pubDate>Wed, 06 Sep 2023 11:34:47 +0000</pubDate>
		<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>&lt;p&gt;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.&lt;/p&gt;

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

&lt;p&gt;Here's the code snippet:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-php"&gt;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-&amp;gt;comment_content, 'Elephant Juice') !== false ) {
        // Set the comment status to 'trash'
        wp_delete_comment( $comment_id );
    }
}
&lt;/code&gt;&lt;/pre&gt;

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

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

&lt;p&gt;But it isn't &lt;em&gt;just&lt;/em&gt; 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):&lt;/p&gt;

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

&lt;p&gt;So you can block comment by User Agent, IP address, and anything else you like. I use it to &lt;a href="https://github.com/pfefferle/wordpress-webmention/issues/343"&gt;filter out specific WebMentions&lt;/a&gt;.&lt;/p&gt;
&lt;img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=46709&amp;amp;HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager"&gt;</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>
