Skip to content

Instantly share code, notes, and snippets.

@ginsterbusch
Created July 29, 2013 08:41
Show Gist options
  • Save ginsterbusch/6103000 to your computer and use it in GitHub Desktop.
Save ginsterbusch/6103000 to your computer and use it in GitHub Desktop.
Enable custom fields for the WordPress comments only for pages/posts with a specific custom field key and value. Quick'n'dirty copy + paste out of a plugin I've been working on (also see the debug inserts) ;) In this case, the custom meta field key is $this->pluginPrefix . 'page' = participant_list_page (that should already tell you what I'm aim…
function is_participant_page() {
global $post;
$return = false;
$arrDebug = array(
'post' => $post,
);
// check custom field
$strIsParticipantsPage = get_post_meta( $post->ID, $this->pluginPrefix . 'page', true );
$arrDebug['post_meta'] = $strIsParticipantsPage;
if( !empty( $strIsParticipantsPage ) && $strIsParticipantsPage == '1' ) {
$return = true;
//echo '<!-- custom post meta: enabled -->';
} else {
//echo '<!-- custom post meta: disabled -->';
}
//echo '<!-- debug: '. print_r( $arrDebug, true ) . ' -->';
return $return;
}
// add a participant to the current list
public function custom_comment_fields( $fields ) {
$return = $fields;
if( $this->is_participant_page() ) {
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$return[ 'author' ] = '<p class="comment-form-author custom-field">'.
'<label for="author">' . __( 'Name' ) . '</label>'.
( $req ? '<span class="required">*</span>' : '' ).
'<input id="author" name="author" type="text" value="'. esc_attr( $commenter['comment_author'] ) .
'" size="30" tabindex="1"' . $aria_req . ' /></p>';
$return[ 'email' ] = '<p class="comment-form-email custom-field">'.
'<label for="email">' . __( 'Email' ) . '</label>'.
( $req ? '<span class="required">*</span>' : '' ).
'<input id="email" name="email" type="text" value="'. esc_attr( $commenter['comment_author_email'] ) .
'" size="30" tabindex="2"' . $aria_req . ' /></p>';
$return[ 'url' ] = '<p class="comment-form-url custom-field">'.
'<label for="url">' . __( 'Website' ) . '</label>'.
'<input id="url" name="url" type="text" value="'. esc_attr( $commenter['comment_author_url'] ) .
'" size="30" tabindex="3" /></p>';
$return[ 'phone' ] = '<p class="comment-form-phone custom-field">'.
'<label for="phone">' . __( 'Phone' ) . '</label>'.
'<input id="phone" name="phone" type="text" size="30" tabindex="4" /></p>';
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment