"Was This Page Helpful?" Buttons
Adds Yes and No buttons at the end of posts and pages so readers can rate them, and shows the helpful score in a column in the admin post list.
Opens WPIDE on
Code
$wpide_helpful_types = array( 'post', 'page' );
add_filter( 'the_content', function ( $content ) use ( $wpide_helpful_types ) {
if ( ! is_singular( $wpide_helpful_types ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$id = get_the_ID();
if ( isset( $_GET['wpide_helpful'] ) && (int) $_GET['wpide_helpful'] === $id ) {
return $content . '<p class="wpide-helpful" role="status">Thanks for your feedback!</p>';
}
$form = '<form class="wpide-helpful" method="post" action="" style="display:flex;gap:.5em;align-items:center;flex-wrap:wrap;margin-top:2em;">';
$form .= '<span>Was this page helpful?</span>';
$form .= '<button type="submit" name="wpide_helpful_vote" value="yes">Yes</button><button type="submit" name="wpide_helpful_vote" value="no">No</button>';
$form .= '<input type="hidden" name="wpide_helpful_post" value="' . esc_attr( $id ) . '">' . wp_nonce_field( 'wpide_helpful', 'wpide_helpful_nonce', false, false ) . '</form>';
return $content . $form;
}, 25 );
add_action( 'template_redirect', function () {
if ( empty( $_POST['wpide_helpful_nonce'] ) || empty( $_POST['wpide_helpful_post'] ) ) {
return;
}
$post_id = absint( $_POST['wpide_helpful_post'] );
$vote = isset( $_POST['wpide_helpful_vote'] ) && 'yes' === $_POST['wpide_helpful_vote'] ? 'yes' : 'no';
$link = get_permalink( $post_id );
if ( ! $link || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wpide_helpful_nonce'] ) ), 'wpide_helpful' ) ) {
return;
}
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
$key = 'wpide_helpful_' . md5( wp_salt() . $post_id . $ip );
if ( ! get_transient( $key ) ) {
$meta = '_wpide_helpful_' . $vote;
update_post_meta( $post_id, $meta, (int) get_post_meta( $post_id, $meta, true ) + 1 );
set_transient( $key, 1, WEEK_IN_SECONDS );
}
wp_safe_redirect( add_query_arg( 'wpide_helpful', $post_id, $link ) );
exit;
} );
foreach ( $wpide_helpful_types as $wpide_type ) {
add_filter( "manage_{$wpide_type}_posts_columns", function ( $columns ) {
$columns['wpide_helpful'] = 'Helpful';
return $columns;
} );
add_action( "manage_{$wpide_type}_posts_custom_column", function ( $column, $post_id ) {
if ( 'wpide_helpful' !== $column ) {
return;
}
$yes = (int) get_post_meta( $post_id, '_wpide_helpful_yes', true );
$no = (int) get_post_meta( $post_id, '_wpide_helpful_no', true );
echo $yes + $no ? esc_html( sprintf( '%d%% (%d votes)', round( $yes / ( $yes + $no ) * 100 ), $yes + $no ) ) : '—';
}, 10, 2 );
}
Notes
Votes are saved as post meta through a nonce-checked request, limited to one per visitor per page; change the post types at the top.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.