. * Also outputs global WebSite schema with Sitelinks Searchbox. */ class AWBSEO_Schema_Output { public static function init() { add_action( 'wp_head', [ __CLASS__, 'output_schema' ], 5 ); } public static function output_schema() { $schemas = []; // Global WebSite schema (once, on every page). $schemas[] = [ '@context' => 'https://schema.org', '@type' => 'WebSite', 'name' => get_bloginfo( 'name' ), 'url' => home_url( '/' ), 'potentialAction' => [ '@type' => 'SearchAction', 'target' => [ '@type' => 'EntryPoint', 'urlTemplate' => home_url( '/?s={search_term_string}' ), ], 'query-input' => 'required name=search_term_string', ], ]; // Breadcrumbs on all pages. $breadcrumb = self::build_breadcrumb(); if ( $breadcrumb ) { $schemas[] = array_merge( [ '@context' => 'https://schema.org' ], $breadcrumb ); } // Post-specific schema. // is_singular() covers single posts, pages, and attachments - including // a static front page in most themes. The elseif catches the edge case // where the homepage is a static page but the theme or a page builder // resets the main query before wp_head fires, causing is_singular() to // return false even though there is a real page_on_front post ID we can // use. It does NOT apply when show_on_front = 'posts' because there is // no single post to attach schema to in that mode. $post_id = 0; if ( is_singular() ) { $post_id = (int) get_the_ID(); } elseif ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) { $post_id = (int) get_option( 'page_on_front' ); } if ( $post_id ) { $post_schema = AWBSEO_Schema_Manager::build( $post_id ); // Lets feature modules (e.g. WooCommerce SEO) enrich schema with // live data instead of only what's in the manual schema_data meta. $post_schema = apply_filters( 'awbseo_post_schema', $post_schema, $post_id ); if ( $post_schema ) { // @graph-style schemas already carry their own @context - use // them as-is so we don't duplicate or overwrite it. if ( ! empty( $post_schema['@context'] ) ) { $schemas[] = $post_schema; } else { $schemas[] = array_merge( [ '@context' => 'https://schema.org' ], $post_schema ); } } } foreach ( $schemas as $schema ) { // JSON_HEX_TAG escapes literal < and > inside encoded string // values (e.g. a post title or category name containing // "") so they can never break out of this \n"; } } private static function build_breadcrumb() { if ( is_front_page() ) return null; $items = [ [ 'name' => __( 'Home', 'adworksbid-seo' ), 'url' => home_url( '/' ) ] ]; if ( is_singular() ) { $post = get_post(); if ( $post->post_type === 'post' ) { $cats = get_the_category( $post->ID ); if ( $cats ) { $items[] = [ 'name' => $cats[0]->name, 'url' => get_category_link( $cats[0] ) ]; } } $items[] = [ 'name' => get_the_title(), 'url' => get_permalink() ]; } elseif ( is_category() || is_tag() || is_tax() ) { $term = get_queried_object(); $items[] = [ 'name' => $term->name, 'url' => get_term_link( $term ) ]; } $list_items = []; foreach ( $items as $i => $item ) { $list_items[] = [ '@type' => 'ListItem', 'position' => $i + 1, 'name' => $item['name'], 'item' => $item['url'], ]; } return [ '@type' => 'BreadcrumbList', 'itemListElement' => $list_items, ]; } }