Last active
February 21, 2020 17:29
-
-
Save thefuxia/4138835 to your computer and use it in GitHub Desktop.
Anonymous comments per post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Anonymous comments per post | |
* Plugin URI: http://wordpress.stackexchange.com/q/73788/73 | |
* Version: 2012.11.24 | |
* Author: Fuxia Scholz | |
* Author URI: https://fuxia.me | |
* Licence: MIT | |
* License URI: http://opensource.org/licenses/MIT | |
*/ | |
add_action( 'post_comment_status_meta_box-options', 'acpp_checkbox' ); | |
/** | |
* Print a checkbox into the comment status metabox. | |
* | |
* @wp-hook post_comment_status_meta_box-options | |
* @param object $post | |
* @return void | |
*/ | |
function acpp_checkbox( $post ) | |
{ | |
$key = '_allow_anonymous_comments'; | |
$current = get_post_meta( $post->ID, $key, TRUE ); | |
printf( | |
'<br /><label for="%1$s"> | |
<input type="checkbox" id="%1$s" name="%1$s" class="selectit" %2$s/> %3$s | |
</label>', | |
$key, | |
checked( 1, $current, FALSE ), | |
apply_filters( 'acpp_metabox_label', 'Allow anonymous comments.' ) | |
); | |
} | |
add_action( 'save_post', 'acpp_save' ); | |
/** | |
* Save the checkbox value as number | |
* | |
* @wp-hook save_post | |
* @param int $post_id | |
* @param object $post | |
* @return void | |
*/ | |
function acpp_save( $post_id, $post ) | |
{ | |
// AJAX autosave | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
// Some other POST request | |
if ( ! isset ( $_POST['post_type'] ) ) | |
return; | |
// Missing capability | |
if ( ! current_user_can( 'edit_' . $_POST['post_type'], $post_id ) ) | |
return; | |
$key = '_allow_anonymous_comments'; | |
// Checkbox successfully clicked | |
if ( isset ( $_POST[ $key ] ) and 'on' === strtolower( $_POST[ $key ] ) ) | |
return update_post_meta( $post_id, $key, 1 ); | |
// Checkbox deselected | |
delete_post_meta( $post_id, $key ); | |
} | |
add_filter( 'pre_option_comment_registration', 'acpp_comment_reg_filter' ); | |
/** | |
* Trick the registration checks on front-end | |
* | |
* Important: If we return FALSE, the filter will be effectively ignored. | |
* It has to be any other value. | |
* | |
* @wp-hook pre_option_comment_registration | |
* @return bool|int | |
*/ | |
function acpp_comment_reg_filter() | |
{ | |
if ( is_admin() ) | |
return FALSE; | |
$key = '_allow_anonymous_comments'; | |
$post_id = 0; | |
// Only available on wp-comments-post.php, not on regular post pages. | |
if ( isset( $_POST['comment_post_ID'] ) ) | |
$post_id = (int) $_POST['comment_post_ID']; | |
// | |
$post = get_post( $post_id ); | |
$open = get_post_meta( $post->ID, $key, TRUE ); | |
if ( 1 == $open ) | |
return 0; | |
return FALSE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment