{"id":73,"date":"2013-05-21T14:03:26","date_gmt":"2013-05-21T18:03:26","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/mypain\/?p=73"},"modified":"2014-05-07T15:21:47","modified_gmt":"2014-05-07T19:21:47","slug":"closing-comments-on-wordpress-multisite","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/mypain\/2013\/05\/21\/closing-comments-on-wordpress-multisite\/","title":{"rendered":"closing comments on wordpress multisite"},"content":{"rendered":"<p>There are some wordpress plugins out there that close comments on blogs as well as enable the admin of the blog to enable it site-wide. I wasn&#8217;t too interested in putting a plugin in place, although I figured if I could just write a code snippit to do this for me I would be happy. The code below is the result of my snippit efforts.<\/p>\n<p>What this does is make a list of all the blogs (assuming your table prefix is wp_) on your multisite install. Then it checks to see if a blog has been blogged on since X months (I have 12 months in here, but you can change that to whatever suits your needs).<\/p>\n<p>Once those conditions are set, the code sets two comment related options. The first is turning on akismet auto-delete of spam. We found that many of the older blogs on our multisite install had multiple tens of thousands of spam comments hanging around &#8211; no need for that. The second is enabling auto-closing of comments on posts older than 60 days (again you can change that to suit your needs).<\/p>\n<p>To run it &#8211; you need to fill in the db info on the top (hostname, db, user, pw) and then just run it with the php cli. I hope this can help out someone and serve as a starting point where I had to start from scratch.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$mysqli = new mysqli(&quot;hostname&quot;, &quot;user&quot;, &quot;password&quot;, &quot;database&quot;);\r\nif ($mysqli-&gt;connect_errno) {\r\necho &quot;Failed to connect to MySQL: (&quot; . $mysqli-&gt;connect_errno . &quot;) &quot; . $mysqli-&gt;connect_error;\r\n}\r\n\r\n# set debug\r\n$debug=1;\r\n\r\n# get all the blogs\r\n$tables_res = $mysqli-&gt;query(&quot;show tables like 'wp_%_options'&quot;);\r\n\r\nwhile ( $tables_row = $tables_res-&gt;fetch_row() ){\r\nlist($blog_wpmu,$blog_id,$blog_options) = explode(&quot;_&quot;, $tables_row&#x5B;0]);\r\n$bloginfo_res = $mysqli-&gt;query(&quot;select * from wp_${blog_id}_options where option_name='siteurl'&quot;);\r\n$bloginfo_row = $bloginfo_res-&gt;fetch_assoc();\r\n$blog_url = $bloginfo_row&#x5B;'option_value'];\r\n\r\n# check to see if blog has been updated in the last year\r\nif ($updated_res = $mysqli-&gt;query(&quot;select id from wp_${blog_id}_posts where post_status = 'publish' and date_add(post_date, interval 12 month) &gt; now() limit 1&quot;)) {\r\n$updated_row_cnt = mysqli_num_rows($updated_res);\r\nif ($updated_row_cnt == 0) {\r\n\r\n# check and set akismet to auto-delete spam\r\n$akismet_res = $mysqli-&gt;query(&quot;select * from wp_${blog_id}_options where option_name='akismet_discard_month'&quot;);\r\n$akismet_row_cnt = mysqli_num_rows($akismet_res);\r\nif ($akismet_row_cnt == 0) {\r\n# set auto-delete\r\n$mysqli-&gt;query(&quot;insert into wp_${blog_id}_options (option_name,option_value,autoload) values ('akismet_discard_month','true','yes')&quot;);\r\n} else {\r\n$akismet_row = $akismet_res-&gt;fetch_assoc();\r\nif ( $akismet_row&#x5B;'option_value'] != &quot;true&quot; || $akismet_row&#x5B;'autoload'] != &quot;yes&quot; ) {\r\n# set auto-delete\r\n$mysqli-&gt;query(&quot;update wp_${blog_id}_options set option_value='true',autoload='yes' where option_name='akismet_discard_month'&quot;);\r\nif ($debug == 1) {\r\necho &quot;akismet set but not enabled on $blog_url ($blog_id)\\n&quot;;\r\n}\r\n}\r\n}\r\n# check and set comments to auto-close\r\n$comments_res = $mysqli-&gt;query(&quot;select * from wp_${blog_id}_options where option_name like 'close_comments_%' order by option_id&quot;);\r\n$comments_row_cnt = mysqli_num_rows($comments_res);\r\nif ($comments_row_cnt == 0) {\r\n# set comments to auto-close\r\n$mysqli-&gt;query(&quot;insert into wp_${blog_id}_options (option_name,option_value,autoload) values ('close_comments_days_old','60','yes')&quot;);\r\n$mysqli-&gt;query(&quot;insert into wp_${blog_id}_options (option_name,option_value,autoload) values ('close_comments_for_old_posts','1','yes')&quot;);\r\n} else {\r\nwhile ( $comments_row = $comments_res-&gt;fetch_assoc()) {\r\nif ( $comments_row&#x5B;'option_name'] == &quot;close_comments_days_old&quot; &amp;amp;&amp;amp; $comments_row&#x5B;'option_value'] != 60 ) {\r\n# set comments to auto-close in 60 days\r\n$mysqli-&gt;query(&quot;update wp_${blog_id}_options set option_value='60',autoload='yes' where option_name='close_comments_days_old'&quot;);\r\nif ($debug == 1) {\r\necho &quot;days_old set to &quot;. $comments_row&#x5B;'option_value'].&quot; on &quot;.$blog_url.&quot; (&quot;.$blog_id.&quot;)\\n&quot;;\r\n}\r\n}\r\nif ( $comments_row&#x5B;'option_name'] == &quot;close_comments_for_old_posts&quot; &amp;amp;&amp;amp; $comments_row&#x5B;'option_value'] == 0 ) {\r\n# set comments to auto-close\r\n$mysqli-&gt;query(&quot;update wp_${blog_id}_options set option_value='1',autoload='yes' where option_name='close_comments_for_old_posts'&quot;);\r\nif ($debug == 1) {\r\necho &quot;old_posts set but not enabled on $blog_url ($blog_id)\\n&quot;;\r\n}\r\n}\r\n}\r\n}\r\n}\r\n}\r\n}\r\n\r\n$mysqli-&gt;close();\r\n\r\n?&gt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are some wordpress plugins out there that close comments on blogs as well as enable the admin of the blog to enable it site-wide. I wasn&#8217;t too interested in putting a plugin in place, although I figured if I could just write a code snippit to do this for me I would be happy. [&hellip;]<\/p>\n","protected":false},"author":758,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,3163,19259],"tags":[],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-code","category-php","category-wpmu","post-preview"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/users\/758"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":4,"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/posts\/73\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/mypain\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}