Halloween Sale 15% off Ends in See the plans
PHP

Test Structured Data From the Admin Bar

Adds a toolbar menu for editors on the front end that opens the current page in Google's Rich Results Test or the Schema.org validator.

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

Code

add_action( 'admin_bar_menu', function ( $bar ) {
    if ( is_admin() || ! current_user_can( 'edit_posts' ) ) {
        return;
    }

    $url = home_url( add_query_arg( array() ) );

    $bar->add_node( array( 'id' => 'wpide-schema-test', 'title' => 'Test schema', 'href' => false ) );
    $bar->add_node( array( 'id' => 'wpide-rich-results', 'parent' => 'wpide-schema-test', 'title' => 'Google Rich Results Test', 'href' => 'https://search.google.com/test/rich-results?url=' . rawurlencode( $url ), 'meta' => array( 'target' => '_blank', 'rel' => 'noopener' ) ) );
    $bar->add_node( array( 'id' => 'wpide-schema-validator', 'parent' => 'wpide-schema-test', 'title' => 'Schema.org Validator', 'href' => 'https://validator.schema.org/#url=' . rawurlencode( $url ), 'meta' => array( 'target' => '_blank', 'rel' => 'noopener' ) ) );
}, 90 );

Notes

The page must be publicly reachable for the testing tools to fetch it.

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.