"Report a Problem on This Page" Form
Adds a small expandable form at the end of posts where readers can report a typo, broken link or outdated information; reports are emailed to the post author with the page link.
Opens WPIDE on
Code
$wpide_report_types = array( 'post' ); // Post types that show the form.
add_filter( 'the_content', function ( $content ) use ( $wpide_report_types ) {
if ( ! is_singular( $wpide_report_types ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$sent = isset( $_GET['wpide_report'] ) && 'sent' === $_GET['wpide_report'];
$form = '<details class="wpide-report"' . ( $sent ? ' open' : '' ) . '><summary>Spotted a mistake? Let us know</summary>';
$form .= $sent ? '<p role="status">Thank you! We will take a look.</p>' : '';
$form .= '<form method="post" action=""><p><label for="wpide-report-type">What is wrong?</label><br><select id="wpide-report-type" name="wpide_report_type"><option>Typo or grammar</option><option>Broken link</option><option>Outdated information</option><option>Something else</option></select></p>';
$form .= '<p><label for="wpide-report-text">Details</label><br><textarea id="wpide-report-text" name="wpide_report_text" rows="3" required maxlength="1000"></textarea></p>';
$form .= '<p style="position:absolute;left:-9999px;" aria-hidden="true"><input name="wpide_report_hp" tabindex="-1" autocomplete="off"></p>';
$form .= '<input type="hidden" name="wpide_report_post" value="' . esc_attr( get_the_ID() ) . '">' . wp_nonce_field( 'wpide_report', 'wpide_report_nonce', false, false );
$form .= '<p><button type="submit">Send report</button></p></form></details>';
return $content . $form;
}, 20 );
add_action( 'template_redirect', function () {
if ( empty( $_POST['wpide_report_nonce'] ) || empty( $_POST['wpide_report_post'] ) ) {
return;
}
$post_id = absint( $_POST['wpide_report_post'] );
$link = get_permalink( $post_id );
if ( ! $link || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wpide_report_nonce'] ) ), 'wpide_report' ) || ! empty( $_POST['wpide_report_hp'] ) ) {
return;
}
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
$key = 'wpide_report_' . md5( wp_salt() . $ip );
$text = sanitize_textarea_field( wp_unslash( isset( $_POST['wpide_report_text'] ) ? $_POST['wpide_report_text'] : '' ) );
$type = sanitize_text_field( wp_unslash( isset( $_POST['wpide_report_type'] ) ? $_POST['wpide_report_type'] : '' ) );
if ( '' !== trim( $text ) && (int) get_transient( $key ) < 5 ) {
$author = get_userdata( (int) get_post_field( 'post_author', $post_id ) );
$to = $author && is_email( $author->user_email ) ? $author->user_email : get_option( 'admin_email' );
wp_mail( $to, 'Problem reported: ' . wp_strip_all_tags( get_the_title( $post_id ) ), "Type: $type\nPage: $link\n\n$text" );
set_transient( $key, (int) get_transient( $key ) + 1, DAY_IN_SECONDS );
}
wp_safe_redirect( add_query_arg( 'wpide_report', 'sent', $link ) . '#content' );
exit;
} );
Notes
Shows on single posts by default (change the post types at the top) and accepts at most five reports per visitor per day.
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.