Halloween Sale 15% off Ends in See the plans
PHP

Add "Reviewed By" and Last Reviewed Date to Yoast's WebPage Schema

Lets editors pick a reviewer and a review date for a post, then adds reviewedBy and lastReviewed to the WebPage piece of the Yoast SEO graph.

Don’t have WPIDE yet? Install it on your site

Code

// Post types that get the reviewer box.
$wpide_review_types = array( 'post', 'page' );

add_action( 'add_meta_boxes', function () use ( $wpide_review_types ) {
    add_meta_box( 'wpide-reviewed-by', 'Expert review', function ( $post ) {
        wp_nonce_field( 'wpide_review_save', 'wpide_review_nonce' );
        $reviewer = (int) get_post_meta( $post->ID, '_wpide_reviewer', true );
        $date     = get_post_meta( $post->ID, '_wpide_reviewed_on', true );

        echo '<p><label for="wpide-reviewer">Reviewed by</label><br>';
        wp_dropdown_users( array( 'name' => 'wpide_reviewer', 'id' => 'wpide-reviewer', 'selected' => $reviewer, 'show_option_none' => '— None —', 'option_none_value' => 0, 'capability' => array( 'edit_posts' ) ) );
        echo '</p><p><label for="wpide-reviewed-on">Last reviewed</label><br>';
        printf( '<input type="date" id="wpide-reviewed-on" name="wpide_reviewed_on" value="%s"></p>', esc_attr( $date ) );
    }, $wpide_review_types, 'side' );
} );

add_action( 'save_post', function ( $post_id ) use ( $wpide_review_types ) {
    if ( ! isset( $_POST['wpide_review_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_review_nonce'] ), 'wpide_review_save' ) ) {
        return;
    }

    if ( wp_is_post_autosave( $post_id ) || ! in_array( get_post_type( $post_id ), $wpide_review_types, true ) || ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    $reviewer = isset( $_POST['wpide_reviewer'] ) ? absint( $_POST['wpide_reviewer'] ) : 0;
    $date     = isset( $_POST['wpide_reviewed_on'] ) ? sanitize_text_field( wp_unslash( $_POST['wpide_reviewed_on'] ) ) : '';

    update_post_meta( $post_id, '_wpide_reviewer', $reviewer );
    update_post_meta( $post_id, '_wpide_reviewed_on', preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date ) ? $date : '' );
} );

add_filter( 'wpseo_schema_webpage', function ( $data ) {
    if ( ! is_array( $data ) || ! is_singular() ) {
        return $data;
    }

    $post_id  = get_queried_object_id();
    $reviewer = (int) get_post_meta( $post_id, '_wpide_reviewer', true );
    $date     = get_post_meta( $post_id, '_wpide_reviewed_on', true );

    if ( $reviewer && get_userdata( $reviewer ) ) {
        $data['reviewedBy'] = array(
            '@type' => 'Person',
            'name'  => get_the_author_meta( 'display_name', $reviewer ),
            'url'   => get_author_posts_url( $reviewer ),
        );
    }

    if ( $date ) {
        $data['lastReviewed'] = $date;
    }

    return $data;
} );

Notes

Useful for health, finance and legal content. The reviewer must be a user on the site.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.