/**
* SamuderaKepri Advanced SEO & Multi-Schema Suite (Anti-Syntax Error)
* Dioptimalkan dengan pembersihan teks ketat untuk portal berita
*/
if ( ! function_exists( 'samudera_seo_master_head' ) ) {
function samudera_seo_master_head() {
if ( ! is_singular( 'post' ) ) {
return;
}
// PENGAMAN: Mencegah eksekusi ganda agar output JSON-LD tidak duplikat
static $schema_rendered = false;
if ( $schema_rendered ) {
return;
}
$schema_rendered = true;
global $post;
if ( empty( $post ) || ! isset( $post->ID ) ) {
return;
}
$post_id = $post->ID;
$post_title = get_the_title( $post_id );
$post_excerpt = get_the_excerpt( $post_id );
// PEMBERSIHAN KETAT: Menghilangkan karakter perusak JSON dari teks berita
$raw_content = wp_strip_all_tags( $post->post_content );
$post_content = preg_replace( '/[\r\n\t]+/', ' ', $raw_content );
$post_content = str_replace( '"', "'", $post_content ); // Ganti kutip ganda jadi kutip satu
$post_content = wp_kses_data( $post_content );
$post_url = esc_url( get_permalink( $post_id ) );
$site_name = sanitize_text_field( get_bloginfo( 'name' ) );
$publish_date = get_the_date( 'c', $post_id );
$modified_date = get_the_modified_date( 'c', $post_id );
$author_name = get_the_author_meta( 'display_name', $post->post_author );
$author_url = get_author_posts_url( $post->post_author );
// Featured Image
$featured_image_url = '';
$img_width = 1024;
$img_height = 683;
if ( has_post_thumbnail( $post_id ) ) {
$thumb_id = get_post_thumbnail_id( $post_id );
$featured_image_url = wp_get_attachment_image_url( $thumb_id, 'large' );
$image_meta = wp_get_attachment_metadata( $thumb_id );
if ( $image_meta ) {
$img_width = isset( $image_meta['width'] ) ? $image_meta['width'] : 1024;
$img_height = isset( $image_meta['height'] ) ? $image_meta['height'] : 683;
}
}
if ( empty( $featured_image_url ) ) {
$featured_image_url = esc_url( get_site_icon_url() );
}
$clean_excerpt = $post_excerpt ? $post_excerpt : wp_trim_words( $post_content, 30 );
$clean_excerpt = str_replace( '"', "'", $clean_excerpt );
// 1. CANONICAL TAG
echo '' . "\n";
// 2. OPEN GRAPH & TWITTER CARDS
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
// 3. MULTI-SCHEMA STRUCTURED DATA (3 Jenis Skema: NewsArticle, Organization, BreadcrumbList)
$categories = get_the_category( $post_id );
$primary_category = ! empty( $categories ) ? $categories[0]->name : 'News';
$primary_cat_url = ! empty( $categories ) ? get_category_link( $categories[0]->term_id ) : home_url( '/' );
$tags = get_the_tags( $post_id );
$keywords = '';
if ( ! empty( $tags ) ) {
$tag_names = wp_list_pluck( $tags, 'name' );
$keywords = implode( ', ', array_slice( $tag_names, 0, 5 ) );
}
$logo_url = esc_url( get_site_icon_url() );
if ( empty( $logo_url ) ) {
$logo_url = esc_url( home_url( '/wp-content/uploads/2026/06/cropped-logo-sk-icon.webp' ) );
}
// Skema 1: NewsArticle
$news_schema = array(
'@context' => 'https://schema.org',
'@type' => 'NewsArticle',
'@id' => $post_url . '#newsarticle',
'headline' => sanitize_text_field( $post_title ),
'description' => $clean_excerpt,
'articleBody' => $post_content,
'datePublished' => esc_attr( $publish_date ),
'dateModified' => esc_attr( $modified_date ),
'inLanguage' => 'id-ID',
'isAccessibleForFree' => true,
'articleSection' => sanitize_text_field( $primary_category ),
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => $post_url
),
'author' => array(
'@type' => 'Person',
'name' => sanitize_text_field( $author_name ),
'url' => esc_url( $author_url )
),
'publisher' => array(
'@type' => 'Organization',
'name' => sanitize_text_field( $site_name ),
'url' => esc_url( home_url( '/' ) ),
'logo' => array(
'@type' => 'ImageObject',
'url' => $logo_url,
'width' => 200,
'height' => 200
)
)
);
if ( ! empty( $featured_image_url ) ) {
$news_schema['image'] = array(
'@type' => 'ImageObject',
'url' => esc_url( $featured_image_url ),
'width' => absint( $img_width ),
'height' => absint( $img_height )
);
}
if ( ! empty( $keywords ) ) {
$news_schema['keywords'] = esc_attr( $keywords );
}
// Skema 2: Organization
$org_schema = array(
'@context' => 'https://schema.org',
'@type' => 'Organization',
'name' => sanitize_text_field( $site_name ),
'url' => esc_url( home_url( '/' ) ),
'logo' => array(
'@type' => 'ImageObject',
'url' => $logo_url,
'width' => 200,
'height' => 200
)
);
// Skema 3: BreadcrumbList
$breadcrumb_schema = array(
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => array(
array(
'@type' => 'ListItem',
'position' => 1,
'name' => 'Beranda',
'item' => esc_url( home_url( '/' ) )
),
array(
'@type' => 'ListItem',
'position' => 2,
'name' => sanitize_text_field( $primary_category ),
'item' => esc_url( $primary_cat_url )
),
array(
'@type' => 'ListItem',
'position' => 3,
'name' => sanitize_text_field( $post_title ),
'item' => $post_url
)
)
);
// Gabungkan 3 Skema
$combined_schema = array(
'@context' => 'https://schema.org',
'@graph' => array(
$news_schema,
$org_schema,
$breadcrumb_schema
)
);
$json_ld = wp_json_encode( $combined_schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT );
if ( json_last_error() === JSON_ERROR_NONE && ! empty( $json_ld ) ) {
echo '' . "\n";
}
}
add_action( 'wp_head', 'samudera_seo_master_head', 5 );
}