Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress comment hacks
//start by checking for a couple of key pieces of info
$current_user = wp_get_current_user();
$private_comments = get_post_meta($post->ID, 'private_comments', true);
$meta_lock = get_post_meta($post->ID, 'meta_lock', true);
$time_lock = get_post_meta($post->ID, 'time_lock', true);
//if the post has the custom field 'meta_lock'
if ($meta_lock) {
//check the current user for that meta field
$user_meta_key = get_user_meta($current_user->ID, $meta_lock, true);
if ($current_user->ID AND $user_meta_key) {
//display the comment form
comment_form();
//give an error message (add some flare!)
} else { echo 'Special users only!'; }
//if the post has the custom field 'time_lock'
} elseif ($time_lock) {
//get the age of the user and prepare the age limits
$user_age=date("Y-m-d", strtotime($current_user->user_registered));
$age_limit = strtotime($time_lock);
$account_age = strtotime($user_age);
//if the current user is OLDER than the set limit
if ($current_user->ID AND $account_age <= $age_limit) {
//display the comment form
comment_form();
//give an error message (add some flare!)
} else { echo 'Users older than '.$user_age.' only!'; }
//if the post has the field 'private_comments'
} elseif ($private_comments) {
//check if the user has an ID (aka is signed in)
if ( $current_user->ID ) {
//display the comment form
comment_form();
//give an error message (add some flare!)
} else { echo 'Users only!'; }
//no special conditions met, just display the comment form
} else { comment_form(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.