Files

121 lines
4.7 KiB
PHP

<?php defined( 'ABSPATH' ) || exit; ?>
<?php
/**
* Modul Native SEO & Structured Data Schema EduKarsa
* 100% Native tanpa plugin eksternal
*/
function edukarsa_native_seo_tags() {
global $post;
$site_name = get_bloginfo( 'name' );
$site_desc = get_bloginfo( 'description' );
$page_url = is_front_page() ? home_url( '/' ) : get_permalink();
$page_title = wp_get_document_title();
$logo_url = get_option( 'edukarsa_header_logo' );
$seo_desc = $site_desc;
$seo_img = $logo_url;
$og_type = 'website';
if ( is_singular() && ! empty( $post ) ) {
$og_type = 'article';
// Deskripsi otomatis dari excerpt atau cuplikan paragraf pertama
if ( ! empty( $post->post_excerpt ) ) {
$seo_desc = strip_tags( $post->post_excerpt );
} else {
$content_text = strip_shortcodes( $post->post_content );
$content_text = wp_strip_all_tags( $content_text );
$seo_desc = wp_trim_words( $content_text, 25, '...' );
}
// Gambar OpenGraph dari Featured Image
if ( has_post_thumbnail( $post->ID ) ) {
$thumb_id = get_post_thumbnail_id( $post->ID );
$img_data = wp_get_attachment_image_src( $thumb_id, 'large' );
if ( $img_data ) {
$seo_img = $img_data[0];
}
}
}
$seo_desc = esc_attr( trim( preg_replace( '/\s+/', ' ', $seo_desc ) ) );
?>
<!-- Native SEO Metadata EduKarsa -->
<meta name="description" content="<?php echo $seo_desc; ?>">
<link rel="canonical" href="<?php echo esc_url( $page_url ); ?>">
<!-- Open Graph Data (Facebook, WhatsApp, Telegram) -->
<meta property="og:locale" content="<?php echo esc_attr( get_locale() ); ?>">
<meta property="og:type" content="<?php echo esc_attr( $og_type ); ?>">
<meta property="og:title" content="<?php echo esc_attr( $page_title ); ?>">
<meta property="og:description" content="<?php echo $seo_desc; ?>">
<meta property="og:url" content="<?php echo esc_url( $page_url ); ?>">
<meta property="og:site_name" content="<?php echo esc_attr( $site_name ); ?>">
<?php if ( ! empty( $seo_img ) ) : ?>
<meta property="og:image" content="<?php echo esc_url( $seo_img ); ?>">
<meta property="og:image:alt" content="<?php echo esc_attr( $page_title ); ?>">
<?php endif; ?>
<!-- Twitter / X Card Data -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="<?php echo esc_attr( $page_title ); ?>">
<meta name="twitter:description" content="<?php echo $seo_desc; ?>">
<?php if ( ! empty( $seo_img ) ) : ?>
<meta name="twitter:image" content="<?php echo esc_url( $seo_img ); ?>">
<?php endif; ?>
<!-- Google Structured Data (JSON-LD Schema) -->
<script type="application/ld+json">
<?php if ( is_singular( 'post' ) ) : ?>
{
"@context": "https://schema.org",
"@type": "NewsArticle",
"headline": "<?php echo esc_js( get_the_title() ); ?>",
"description": "<?php echo esc_js( $seo_desc ); ?>",
"url": "<?php echo esc_url( $page_url ); ?>",
"datePublished": "<?php echo esc_js( get_the_date( 'c' ) ); ?>",
"dateModified": "<?php echo esc_js( get_the_modified_date( 'c' ) ); ?>",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "<?php echo esc_url( $page_url ); ?>"
},
<?php if ( ! empty( $seo_img ) ) : ?>
"image": "<?php echo esc_url( $seo_img ); ?>",
<?php endif; ?>
"author": {
"@type": "Person",
"name": "<?php echo esc_js( get_the_author() ); ?>"
},
"publisher": {
"@type": "EducationalOrganization",
"name": "<?php echo esc_js( $site_name ); ?>",
"logo": {
"@type": "ImageObject",
"url": "<?php echo esc_url( $logo_url ); ?>"
}
}
}
<?php else : ?>
{
"@context": "https://schema.org",
"@type": "EducationalOrganization",
"name": "<?php echo esc_js( $site_name ); ?>",
"url": "<?php echo esc_url( home_url( '/' ) ); ?>",
"description": "<?php echo esc_js( $site_desc ); ?>",
<?php if ( ! empty( $logo_url ) ) : ?>
"logo": "<?php echo esc_url( $logo_url ); ?>",
<?php endif; ?>
"sameAs": [
"<?php echo esc_url( get_option( 'edukarsa_footer_fb' ) ); ?>",
"<?php echo esc_url( get_option( 'edukarsa_footer_ig' ) ); ?>",
"<?php echo esc_url( get_option( 'edukarsa_footer_yt' ) ); ?>"
]
}
<?php endif; ?>
</script>
<?php
}
add_action( 'wp_head', 'edukarsa_native_seo_tags', 1 );