
Code Snipet Multi Bahasa
Sebelum mulai langkah-langkah injeksi Code Snipet Multi Bahasa Gratis Tanpa plugin wpml polylang sangat direkomendasikan untuk menginstal plugin Wpcode.
Table Of Contents
1.Registry Core (CPT & Hreflang):
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://pujashanti.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://pujashanti.web.id/" />' . "\n";
echo '<link rel="alternate" hreflang="x-default" href="https://pujashanti.web.id/" />' . "\n";
} else {
echo '<link rel="alternate" hreflang="id" href="' . esc_url($current_url) . '" />' . "\n";
echo '<link rel="alternate" hreflang="en" href="https://pujashanti.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);
2. Rewrite English Home:
Menghilangkan error 404 pada URL /en/ dan mendukung navigasi AMP.
/**
* SNIPPET 2: THE URL ENGINE & AMP FIX
* Website: pujashanti.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);
3. Custom Field Taxonomy:
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']));
}
}
4. Language Switcher Post & Page:
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" onclick="togglePsLang()">
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M12.87 15.07l-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z"/></svg>
<span class="ps-code"><?php echo $label; ?></span> <span>Language</span>
</button>
</div>
<?php
});
5. Language Switcher Khusus Taksonomi:
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" onclick="togglePsLang()">
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M12.87 15.07l-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z"/></svg>
<span class="ps-code"><?php echo $label; ?></span> <span>Language</span>
</button>
</div>
<?php
});
6. Language Switcher Blog Index:
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://pujashanti.web.id/en/articles/">🇺🇸 English (EN)</a>'; ?>
<?php if ($is_en) echo '<a href="https://pujashanti.web.id/artikel/">🇮🇩 Indonesia (ID)</a>'; ?>
</div>
<button type="button" class="ps-lang-toggle" onclick="togglePsLang()">
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M12.87 15.07l-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z"/></svg>
<span class="ps-code"><?php echo $label; ?></span> <span>Language</span>
</button>
</div>
<?php
});
7. CSS All-in-One Switcher:
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);
8. CSS Khusus AMP:
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 !important;
bottom: 120px !important; /* Jarak 70px dari tepi bawah (di atas Social Share) */
right: 15px !important; /* Jarak dari tepi kanan */
z-index: 999999 !important;
display: block !important;
background: transparent !important;
pointer-events: none !important;
top: auto !important; /* Mematikan setting top sebelumnya */
}
/* Tombol ID/EN Kapsul Modern */
.ps-amp-top-btn {
pointer-events: auto !important;
display: inline-block !important;
background-color: #ffffff !important;
color: #003366 !important; /* Navy Pujashanti */
padding: 10px 18px !important;
border-radius: 50px !important;
font-size: 13px !important;
font-weight: 800 !important;
text-decoration: none !important;
border: 2px solid #b38b4d !important; /* Border Emas */
box-shadow: 0 6px 15px rgba(0,0,0,0.3) !important;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important;
line-height: 1 !important;
white-space: nowrap !important;
transition: transform 0.2s ease !important;
}
/* Efek Klik */
.ps-amp-top-btn:active {
transform: scale(0.9) !important;
}
<?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>';
}
}
9. Language Switcher Khusus AMPforWP:
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 !important;
bottom: 120px !important; /* Jarak 70px dari tepi bawah (di atas Social Share) */
right: 15px !important; /* Jarak dari tepi kanan */
z-index: 999999 !important;
display: block !important;
background: transparent !important;
pointer-events: none !important;
top: auto !important; /* Mematikan setting top sebelumnya */
}
/* Tombol ID/EN Kapsul Modern */
.ps-amp-top-btn {
pointer-events: auto !important;
display: inline-block !important;
background-color: #ffffff !important;
color: #003366 !important; /* Navy Pujashanti */
padding: 10px 18px !important;
border-radius: 50px !important;
font-size: 13px !important;
font-weight: 800 !important;
text-decoration: none !important;
border: 2px solid #b38b4d !important; /* Border Emas */
box-shadow: 0 6px 15px rgba(0,0,0,0.3) !important;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important;
line-height: 1 !important;
white-space: nowrap !important;
transition: transform 0.2s ease !important;
}
/* Efek Klik */
.ps-amp-top-btn:active {
transform: scale(0.9) !important;
}
<?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>';
}
}
Langkah Final: Reset Permalink (Wajib)
Setelah semua kode dan halaman siap, Anda WAJIB melakukan reset permalink. Jika tidak, struktur URL baru Anda akan menemui error 404.
Cara Melakukannya:
- Buka menu Settings (Pengaturan) > Permalinks.
- Tanpa mengubah pengaturan apapun, klik tombol Save Changes (Simpan Perubahan).
- Ulangi sekali lagi (Total 2x klik simpan).
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



Tinggalkan Balasan