Custom Post Types
Apa Itu Post Type?
Di WordPress, semua konten pada dasarnya adalah post dengan label (post type) yang berbeda. Secara default, WordPress hanya menyediakan 2 post type:
| Post Type | Keterangan |
|---|---|
post | Post blog biasa |
page | Halaman statis |
Sebagai developer, kita bisa membuat custom post type baru sesuai kebutuhan — misalnya: Events, Programs, Professors, Campuses.
Langkah 1: Mendaftarkan Custom Post Type
Buka file functions.php di folder tema kamu, lalu tambahkan kode berikut di bagian bawah:
function university_post_types() {
register_post_type('event', array(
'public' => true
));
}
add_action('init', 'university_post_types');| Bagian | Penjelasan |
|---|---|
add_action('init', ...) | Hook ke event init — saat WordPress inisialisasi. Untuk custom post type, harus pakai init (bukan after_setup_theme atau wp_enqueue_scripts) |
register_post_type('event', ...) | Fungsi WordPress untuk mendaftarkan post type baru bernama event |
'public' => true | Membuat post type terlihat di admin dan frontend |
Setelah disimpan, akan muncul item baru di sidebar admin (tapi masih berlabel "Posts").
Langkah 2: Menambahkan Labels
Agar sidebar menampilkan nama yang benar:
function university_post_types() {
register_post_type('event', array(
'public' => true,
'labels' => array(
'name' => 'Events',
'add_new_item' => 'Add New Event',
'edit_item' => 'Edit Event',
'all_items' => 'All Events',
'singular_name' => 'Event'
)
));
}
add_action('init', 'university_post_types');| Parameter Label | Fungsi |
|---|---|
name | Nama yang tampil di sidebar |
add_new_item | Teks heading saat membuat post baru |
edit_item | Teks heading saat mengedit post |
all_items | Teks di submenu sidebar (hover) |
singular_name | Nama bentuk tunggal |
💡 Untuk daftar lengkap label, Google: register_post_type → buka codex.wordpress.org
Langkah 3: Menambahkan Custom Icon
Gunakan ikon dari WordPress Dashicons (https://developer.wordpress.org/resource/dashicons/):
register_post_type('event', array(
'public' => true,
'labels' => array(
'name' => 'Events',
'add_new_item' => 'Add New Event',
'edit_item' => 'Edit Event',
'all_items' => 'All Events',
'singular_name' => 'Event'
),
'menu_icon' => 'dashicons-calendar'
));Cari ikon yang sesuai di halaman Dashicons, klik ikon → salin code name-nya (contoh: dashicons-calendar).
Langkah 4: Pindahkan ke Must-Use Plugins
Masalah dengan functions.php
Kode di functions.php terikat pada tema. Jika tema diganti, custom post type hilang dari sidebar dan konten tidak bisa diakses (walau data tetap aman di database).
Solusi: Must-Use Plugins
- Buka folder instalasi WordPress:
wp-content/ - Buat folder baru:
mu-plugins - Buat file baru di dalamnya:
university-post-types.php - Cut kode dari
functions.phpdan paste ke file baru
<?php
function university_post_types() {
register_post_type('event', array(
'public' => true,
'labels' => array(
'name' => 'Events',
'add_new_item' => 'Add New Event',
'edit_item' => 'Edit Event',
'all_items' => 'All Events',
'singular_name' => 'Event'
),
'menu_icon' => 'dashicons-calendar'
));
}
add_action('init', 'university_post_types');| Fitur | Plugin Biasa | Must-Use Plugin |
|---|---|---|
| Lokasi | wp-content/plugins/ | wp-content/mu-plugins/ |
| Bisa dinonaktifkan? | ✅ Ya | ❌ Tidak — selalu aktif |
| Cocok untuk | Fitur opsional | Custom post types, fitur kritis |
⚠️ Penting: Must-use plugins tidak bisa dinonaktifkan. Selama file PHP ada di folder
mu-plugins, WordPress akan selalu memuatnya. Ini memastikan pemilik website tidak pernah kehilangan akses ke custom post type mereka.
Struktur File Saat Ini
wp-content/
├── mu-plugins/
│ └── university-post-types.php ← Kode custom post type
├── plugins/
├── themes/
│ └── developer-developer/
│ ├── functions.php ← Hapus kode CPT dari sini
│ ├── front-page.php
│ ├── single.php
│ └── ...Buat Event Contoh
Setelah kode disimpan, buat beberapa event di WordPress admin:
- Math Meetup
- The Science of Cats
- Poetry Day
Masing-masing dengan isi dummy content.