Sebelum mulai langkah-langkah injeksi Code Snipet Multi Bahasa Gratis Tanpa plugin wpml polylang sangat direkomendasikan untuk menginstal plugin Wpcode.
Mendaftarkan CPT English dan menyuntikkan meta tag <hreflang> agar Google mengenali relasi bahasa secara resmi.
Tambahkan kode baru [php] berikut menggunakan snipet wpcode:
<?php
/**
* WP Custom Multilingual CPT & Dynamic Hreflang
* * Description: Registers English Custom Post Types (Articles & Pages) and
* dynamically injects SEO Hreflang tags and language attributes.
* * @package Pujashanti Language Core
* @version 1.0.0
* @author Pujashanti (https://semaramedia.web.id/
* @license MIT License
* * Copyright (c) 2026 Pujashanti
* * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* * The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/
// 1. REGISTRASI TAXONOMY & CPT
add_action('init', function() {
// Kategori Inggris
register_taxonomy('english_category', array('english_content'), array(
'hierarchical' => true,
'labels' => array('name' => 'Categories (EN)'),
'public' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'en/topic', 'with_front' => false),
));
// Artikel Inggris
register_post_type('english_content', array(
'labels' => array('name' => 'English Articles'),
'public' => true,
'has_archive' => 'en/articles',
'rewrite' => array('slug' => 'en/articles', 'with_front' => false),
'menu_icon' => 'dashicons-translation',
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'),
'show_in_rest' => true,
'taxonomies' => array('english_category'),
));
// Halaman Inggris
register_post_type('english_page', array(
'labels' => array('name' => 'English Pages'),
'public' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'en', 'with_front' => false, 'pages' => true),
'menu_icon' => 'dashicons-admin-page',
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes', 'custom-fields'),
'show_in_rest' => true,
));
}, 5);
// 2. FUNGSI DETEKSI KONTEKS (Helper)
if ( ! function_exists( 'ps_is_english_context' ) ) {
/**
* Checks if the current request is within the English language context.
* @return boolean
*/ function ps_is_english_context() {
global $post;
$en_types = array('english_content', 'english_page');
$uri = $_SERVER['REQUEST_URI'] ?? '';
if ( (isset($post) && in_array($post->post_type, $en_types)) ||
is_tax('english_category') ||
strpos($uri, '/en/') !== false ) {
return true;
}
return false;
}
}
// 3. DYNAMIC HREFLANG INJECTION
add_action('wp_head', 'ps_dynamic_hreflang', 1);
add_action('amp_post_template_head', 'ps_dynamic_hreflang', 1);
function ps_dynamic_hreflang() {
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (ps_is_english_context()) {
echo '<link rel="alternate" hreflang="en" href="' . esc_url($current_url) . '" />' . "\n";
echo '<link rel="alternate" hreflang="id" href="https://semaramedia.web.id/" />' . "\n";
echo '<link rel="alternate" hreflang="x-default" href="https://semaramedia.web.id/" />' . "\n";
} else {
echo '<link rel="alternate" hreflang="id" href="' . esc_url($current_url) . '" />' . "\n";
echo '<link rel="alternate" hreflang="en" href="https://semaramedia.web.id/en/" />' . "\n";
echo '<link rel="alternate" hreflang="x-default" href="' . esc_url($current_url) . '" />' . "\n";
}
}
// 4. FORCE LANG ATTRIBUTE (HTML Lang Tag)
add_action('template_redirect', function() {
if (ps_is_english_context()) {
ob_start(function($buffer) {
$buffer = preg_replace('/lang="id(-|_)?[A-Z]*"/i', 'lang="en-US"', $buffer);
return $buffer;
});
}
}, 1); Menghilangkan error 404 pada URL /en/ dan mendukung navigasi AMP.
/**
* SNIPPET 2: THE URL ENGINE & AMP FIX
* Website: semaramedia.web.id/
*/
// 1. REWRITE RULES (Jalur Sistem)
add_action('init', function() {
// Pastikan /en/ dan /en/amp/ mengarah ke halaman 'home' di English Pages
add_rewrite_rule('^en/?$', 'index.php?english_page=home', 'top');
add_rewrite_rule('^en/amp/?$', 'index.php?english_page=home&=1', 'top');
}, 10);
// 2. THE GHOST HOME LOGIC (Tanpa Redirect)
add_action('parse_request', function($wp) {
if (isset($wp->request) && $wp->request == 'en') {
$wp->query_vars['english_page'] = 'home';
$wp->query_vars['post_type'] = 'english_page';
$wp->query_vars['name'] = 'home';
add_filter('status_header', function($status, $code) {
return ($code == 404) ? 'HTTP/1.1 200 OK' : $status;
}, 10, 2);
add_action('wp', function() {
global $wp_query;
if ($wp_query->is_404) {
$wp_query->is_404 = false;
$wp_query->is_single = true;
$wp_query->is_page = true;
}
});
}
});
// 3. AMP INTEGRATION (Fix Mismatch & Canonical)
add_filter('amp_post_status_default_enabled', function($enabled, $post) {
if (isset($post->post_type) && in_array($post->post_type, ['english_page', 'english_content'])) return true;
return $enabled;
}, 10, 2);
add_filter('amp_get_canonical_url', 'ps_fix_clean_canonical', 999);
add_filter('ampforwp_canonical_url', 'ps_fix_clean_canonical', 999);
function ps_fix_clean_canonical($url) {
// Jika di halaman home english, paksa canonical ke /en/
if (strpos($_SERVER['REQUEST_URI'], '/en/') !== false && (is_single('home') || is_front_page())) {
return home_url('/en/');
}
return $url;
}
// 4. INJEKSI AMPHTML PADA DESKTOP /en/
add_action('wp_head', function() {
if (trim($_SERVER['REQUEST_URI'], '/') == 'en') {
echo '<link rel="amphtml" href="' . home_url('/en/?amp=1') . '" />' . "\n";
}
}, 2); Menambahkan fitur input URL relasi khusus untuk editor kategori (Taxonomy) yang secara bawaan tidak didukung WordPress.
/**
* PUJASHANTI: UNIVERSAL TAXONOMY META INPUT
* Otomatis muncul di Kategori Post, Tag, dan Taksonomi CPT English
*/
add_action('admin_init', function() {
// Ambil semua taksonomi yang ada di WordPress
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
// Suntik Form Edit
add_action($taxonomy . '_edit_form_fields', 'ps_taxonomy_edit_meta_fields', 10, 2);
// Suntik Logika Simpan
add_action('edited_' . $taxonomy, 'ps_save_taxonomy_meta', 10, 1);
}
});
// Fungsi Form (Tetap sama, namun pastikan nama function sesuai)
function ps_taxonomy_edit_meta_fields($term, $taxonomy) {
$url_en = get_term_meta($term->term_id, 'url_en', true);
$url_id = get_term_meta($term->term_id, 'url_id', true);
?>
<tr class="form-field">
<th scope="row"><label for="url_en">URL English (EN)</label></th>
<td>
<input type="url" name="url_en" id="url_en" value="<?php echo esc_url($url_en); ?>" placeholder="https://...">
<p class="description">Link versi Inggris untuk taksonomi ini.</p>
</td>
</tr>
<tr class="form-field">
<th scope="row"><label for="url_id">URL Indonesia (ID)</label></th>
<td>
<input type="url" name="url_id" id="url_id" value="<?php echo esc_url($url_id); ?>" placeholder="https://...">
<p class="description">Link versi Indonesia untuk taksonomi ini.</p>
</td>
</tr>
<?php
}
// Fungsi Simpan (Universal)
function ps_save_taxonomy_meta($term_id) {
if (isset($_POST['url_en'])) {
update_term_meta($term_id, 'url_en', esc_url_raw($_POST['url_en']));
}
if (isset($_POST['url_id'])) {
update_term_meta($term_id, 'url_id', esc_url_raw($_POST['url_id']));
}
} Logika otomatis untuk mendeteksi relasi bahasa dan menampilkan tombol switcher di dalam konten.
/**
* SWITCHER 1: SINGLE POST / PAGE / CPT
*/add_action('wp_footer', function() {
if ( is_admin() || !is_singular() || (function_exists('ampforwp_is_amp_endpoint') && ampforwp_is_amp_endpoint()) ) return;
$id = get_the_ID();
$url_en = get_post_meta($id, 'url_en', true);
$url_id = get_post_meta($id, 'url_id', true);
if ( empty($url_en) && empty($url_id) ) return;
$label = !empty($url_en) ? 'ID' : 'EN';
?>
<div class="ps-lang-wrapper">
<div class="ps-lang-menu" id="psLangMenu">
<?php if (!empty($url_en)) echo '<a href="'.esc_url($url_en).'">🇺🇸 English (EN)</a>'; ?>
<?php if (!empty($url_id)) echo '<a href="'.esc_url($url_id).'">🇮🇩 Indonesia (ID)</a>'; ?>
</div>
<button type="button" class="ps-lang-toggle" > Menangani navigasi bahasa khusus untuk halaman arsip/kategori agar tidak konflik dengan post biasa.
/**
* SWITCHER 2: TAXONOMY (CATEGORY / TAG)
*/add_action('wp_footer', function() {
if ( is_admin() || (!is_category() && !is_tag() && !is_tax()) || (function_exists('ampforwp_is_amp_endpoint') && ampforwp_is_amp_endpoint()) ) return;
$term_id = get_queried_object_id();
$url_en = get_term_meta($term_id, 'url_en', true);
$url_id = get_term_meta($term_id, 'url_id', true);
if ( empty($url_en) && empty($url_id) ) return;
$label = !empty($url_en) ? 'ID' : 'EN';
?>
<div class="ps-lang-wrapper">
<div class="ps-lang-menu" id="psLangMenu">
<?php if (!empty($url_en)) echo '<a href="'.esc_url($url_en).'">🇺🇸 English (EN)</a>'; ?>
<?php if (!empty($url_id)) echo '<a href="'.esc_url($url_id).'">🇮🇩 Indonesia (ID)</a>'; ?>
</div>
<button type="button" class="ps-lang-toggle" > Filter khusus untuk halaman daftar artikel (Blogpage) agar navigasi tetap konsisten.
/**
* SWITCHER 3: MAIN ARCHIVE (STATIC)
*/add_action('wp_footer', function() {
if ( is_admin() || is_singular() || is_category() || is_tag() || is_tax() ) return;
$uri = $_SERVER['REQUEST_URI'];
$is_id = (strpos($uri, '/artikel/') !== false);
$is_en = (strpos($uri, '/en/articles/') !== false);
if ( !$is_id && !$is_en ) return;
$label = $is_id ? 'ID' : 'EN';
?>
<div class="ps-lang-wrapper">
<div class="ps-lang-menu" id="psLangMenu">
<?php if ($is_id) echo '<a href="https://semaramedia.web.id/en/articles/">🇺🇸 English (EN)</a>'; ?>
<?php if ($is_en) echo '<a href="https://semaramedia.web.id/artikel/">🇮🇩 Indonesia (ID)</a>'; ?>
</div>
<button type="button" class="ps-lang-toggle" > Desain visual seragam untuk semua tombol switcher (Desktop & Mobile).
/**
* SHARED ASSETS: CSS & JS
*/add_action('wp_footer', function() { ?>
<script>
function togglePsLang() {
var m = document.getElementById("psLangMenu");
if(m) m.classList.toggle("show");
}
window.onclick = function(e) {
if (!e.target.closest('.ps-lang-wrapper')) {
var m = document.getElementById("psLangMenu");
if(m) m.classList.remove('show');
}
}
</script>
<style>
/* Kontainer Utama */ .ps-lang-wrapper {
position: fixed;
bottom: 120px; /* Posisi default (Desktop) - di atas anchor/tombol back to top */ right: 20px;
z-index: 999999; /* Z-index sangat tinggi agar tidak tertutup elemen lain */ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
/* Tombol Switcher */ .ps-lang-toggle {
background: #ffffff;
border: 1.5px solid #2c3e50;
padding: 8px 14px;
border-radius: 50px;
cursor: pointer;
display: flex;
align-items: center;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
transition: all 0.2s ease;
}
.ps-lang-toggle:hover {
background: #f8f9fa;
transform: translateY(-2px);
}
.ps-lang-toggle svg {
color: #2c3e50;
}
.ps-lang-toggle .ps-code {
font-weight: 800;
margin: 0 6px;
color: #2c3e50;
font-size: 14px;
border-right: 1px solid #ddd;
padding-right: 6px;
}
.ps-lang-toggle span:last-child {
font-size: 12px;
color: #666;
font-weight: 600;
text-transform: uppercase;
}
/* Menu Drop-up */ .ps-lang-menu {
display: none;
position: absolute;
bottom: 55px;
right: 0;
background: #ffffff;
border: 1px solid #ddd;
border-radius: 12px;
min-width: 180px;
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
overflow: hidden;
animation: psFadeInUp 0.3s ease;
}
@keyframes psFadeInUp {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.ps-lang-menu.show { display: block; }
.ps-lang-menu a {
display: block;
padding: 12px 18px;
text-decoration: none;
color: #333;
border-bottom: 1px solid #f0f0f0;
font-size: 14px;
font-weight: 500;
transition: background 0.2s;
}
.ps-lang-menu a:last-child { border: none; }
.ps-lang-menu a:hover { background: #f1f3f5; color: #2c3e50; }
/* RESPONSIVE: KHUSUS MOBILE (Menghindari Iklan Anchor 80px) */ @media screen and (max-width: 768px) {
.ps-lang-wrapper {
bottom: 120px; /* Jarak aman di atas iklan anchor 80px */ right: 15px;
}
.ps-lang-toggle {
padding: 6px 12px;
}
.ps-lang-toggle span:last-child {
display: none; /* Sembunyikan teks "Language" di mobile agar ringkas, sisakan ikon & ID/EN */ }
.ps-lang-toggle .ps-code {
border-right: none;
margin-right: 0;
padding-right: 0;
}
}
</style>
<?php }, 100); Kode gaya yang sudah difilter agar lolos validasi ketat sistem AMP.
/**
* 1. CSS KHUSUS AMPFORWP (Suntik ke Head)
* Penempatan: Pojok Kanan Bawah (Floating)
*/add_action('amp_post_template_css', function() {
?>
/* Container Switcher - Melayang di Kanan Bawah */ .ps-amp-top-switcher {
position: fixed ;
   bottom: 120px ; /* Jarak 70px dari tepi bawah (di atas Social Share) */ right: 15px ; /* Jarak dari tepi kanan */ z-index: 999999 ;
display: block ;
background: transparent ;
pointer-events: none ;
top: auto ; /* Mematikan setting top sebelumnya */ }
/* Tombol ID/EN Kapsul Modern */ .ps-amp-top-btn {
pointer-events: auto ;
display: inline-block ;
background-color: #ffffff ;
color: #003366 ; /* Navy Pujashanti */ padding: 10px 18px ;
border-radius: 50px ;
font-size: 13px ;
font-weight: 800 ;
text-decoration: none ;
border: 2px solid #b38b4d ; /* Border Emas */ box-shadow: 0 6px 15px rgba(0,0,0,0.3) ;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif ;
line-height: 1 ;
white-space: nowrap ;
transition: transform 0.2s ease ;
}
/* Efek Klik */ .ps-amp-top-btn:active {
transform: scale(0.9) ;
}
<?php
});
/**
* 2. HTML SWITCHER (Suntik Setelah Header)
* Menggunakan hook internal AMPforWP
*/add_action('ampforwp_after_header', 'ps_header_switcher_bottom_safe');
function ps_header_switcher_bottom_safe() {
$post_id = get_the_ID();
$current_uri = $_SERVER['REQUEST_URI'];
$link = '';
// Logika deteksi URL English menggunakan Meta Data
if (strpos($current_uri, '/en/') !== false) {
$url_id = get_post_meta($post_id, 'url_id', true);
if ($url_id) {
$link = '<a href="' . esc_url($url_id) . '?amp=1" class="ps-amp-top-btn">🇮🇩 INDONESIA</a>';
}
} else {
$url_en = get_post_meta($post_id, 'url_en', true);
if ($url_en) {
$link = '<a href="' . esc_url($url_en) . '?amp=1" class="ps-amp-top-btn">🇺🇸 ENGLISH</a>';
}
}
if ($link) {
echo '<div class="ps-amp-top-switcher">' . $link . '</div>';
}
} Optimasi akhir agar navigasi bahasa berjalan sempurna di ekosistem AMP.
/**
* 1. CSS KHUSUS AMPFORWP (Suntik ke Head)
* Penempatan: Pojok Kanan Bawah (Floating)
*/add_action('amp_post_template_css', function() {
?>
/* Container Switcher - Melayang di Kanan Bawah */ .ps-amp-top-switcher {
position: fixed ;
   bottom: 120px ; /* Jarak 70px dari tepi bawah (di atas Social Share) */ right: 15px ; /* Jarak dari tepi kanan */ z-index: 999999 ;
display: block ;
background: transparent ;
pointer-events: none ;
top: auto ; /* Mematikan setting top sebelumnya */ }
/* Tombol ID/EN Kapsul Modern */ .ps-amp-top-btn {
pointer-events: auto ;
display: inline-block ;
background-color: #ffffff ;
color: #003366 ; /* Navy Pujashanti */ padding: 10px 18px ;
border-radius: 50px ;
font-size: 13px ;
font-weight: 800 ;
text-decoration: none ;
border: 2px solid #b38b4d ; /* Border Emas */ box-shadow: 0 6px 15px rgba(0,0,0,0.3) ;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif ;
line-height: 1 ;
white-space: nowrap ;
transition: transform 0.2s ease ;
}
/* Efek Klik */ .ps-amp-top-btn:active {
transform: scale(0.9) ;
}
<?php
});
/**
* 2. HTML SWITCHER (Suntik Setelah Header)
* Menggunakan hook internal AMPforWP
*/add_action('ampforwp_after_header', 'ps_header_switcher_bottom_safe');
function ps_header_switcher_bottom_safe() {
$post_id = get_the_ID();
$current_uri = $_SERVER['REQUEST_URI'];
$link = '';
// Logika deteksi URL English menggunakan Meta Data
if (strpos($current_uri, '/en/') !== false) {
$url_id = get_post_meta($post_id, 'url_id', true);
if ($url_id) {
$link = '<a href="' . esc_url($url_id) . '?amp=1" class="ps-amp-top-btn">🇮🇩 INDONESIA</a>';
}
} else {
$url_en = get_post_meta($post_id, 'url_en', true);
if ($url_en) {
$link = '<a href="' . esc_url($url_en) . '?amp=1" class="ps-amp-top-btn">🇺🇸 ENGLISH</a>';
}
}
if ($link) {
echo '<div class="ps-amp-top-switcher">' . $link . '</div>';
}
} Setelah semua kode dan halaman siap, Anda WAJIB melakukan reset permalink. Jika tidak, struktur URL baru Anda akan menemui error 404.
Cara Melakukannya:
Mengapa harus 2 kali?
Langkah ini memaksa WordPress menghapus cache struktur URL lama dan menulis ulang (flush) aturan navigasi yang baru ke dalam file .htaccess. Pengulangan kedua memastikan sistem benar-benar telah menyinkronkan seluruh aturan Rewrite URL dari Custom Post Type yang baru saja kita buat.
Halaman ini tidak mendukung versi amp
Lihat Juga
Berikut adalah artikel lengkap mengenai Strategi Pertahanan Berlapis: Mengamankan Website dengan Cloudflare WAF. Strategi penerapan…
Strategi Keamanan Web: MengapaTrafik ASN Pusat Data Wajib Di-Challenge? Dalam pengelolaan strategi keamanan web modern,…
Membuat Security Rules Cloudflare adalah langkah krusial untuk melindungi situs WordPress dari serangan brute force,…
Arsitektur Modern & Performa Tinggi Halaman ini dibangun menggunakan framework Next JS dan dideploy melalui…
Panduan Pro: Membuat WordPress Multi-Bahasa Tanpa Plugin (WPML/Polylang) Cara Membuat WordPress Multi bahasa Tanpa Plugin…
Panduan Penggunaan Plugin Custom Animation Bundle (CAB) Versi: 1.1.3 (Updated 2026) Panduan Penggunaan Plugin Custom…