Skip to content

Block Restrictions & Blank Template untuk Landing Page ​

Membatasi Block Types per Environment ​

Konsep ​

WordPress memungkinkan kita membatasi block types mana yang boleh dipakai berdasarkan environment:

  • Page/Post editor — editing konten blog/page biasa
  • Full Site Editor (FSE) — editing template keseluruhan

Filter: allowed_block_types_all ​

php
// functions.php
add_filter('allowed_block_types_all', 'myAllowedBlocks', 10, 2);

function myAllowedBlocks($allowed_block_types, $editor_context) {
    // Jika sedang di page/post editor
    if (!empty($editor_context->post)) {
        return $allowed_block_types; // Izinkan semua block
    }
    
    // Jika sedang di full site editor template
    return array(
        'ourBlockTheme/header',
        'ourBlockTheme/footer',
        'ourBlockTheme/banner',
        'ourBlockTheme/slideshow',
        'ourBlockTheme/eventsAndBlogs',
        'ourBlockTheme/singlePost',
        'ourBlockTheme/page'
        // ... daftar block yang diizinkan
    );
}

Parameter ​

ParameterFungsi
$allowed_block_typesArray block types yang diizinkan (default: semua)
$editor_contextObject konteks editor
$editor_context->postObject post (ada jika di page/post editor, null jika di FSE)

Logika ​

$editor_context->post ada?
  ├── YA  → Sedang di page/post editor → return semua block
  └── TIDAK → Sedang di FSE → return restricted array

Membatasi Berdasarkan Post Type ​

Contoh: Hanya untuk Professor ​

php
function myAllowedBlocks($allowed_block_types, $editor_context) {
    if (!empty($editor_context->post)) {
        // Hanya restrict untuk post type "professor"
        if ($editor_context->post->post_type === 'professor') {
            return array(
                'core/paragraph',
                'core/heading',
                'core/list',
                'core/image'
            );
        }
        return $allowed_block_types; // Post type lain = semua block
    }
    
    // FSE = custom blocks saja
    return array(
        'ourBlockTheme/header',
        'ourBlockTheme/footer',
        // ...
    );
}

Core Block Names ​

Block bawaan WordPress menggunakan prefix core/:

BlockName
Paragraphcore/paragraph
Headingcore/heading
Listcore/list
Imagecore/image
Buttoncore/button
Tablecore/table

Blank Template untuk Landing Pages ​

Konsep ​

Buat template kosong — tanpa header, tanpa footer — yang bisa dipilih per halaman. Cocok untuk landing pages atau halaman custom yang unik.

Langkah 1: Buat Template File ​

templates/emptycanvas.html

html
<!-- wp:post-content /-->

Hanya satu block: wp:post-content — menampilkan konten yang ditulis user di editor.

Langkah 2: Daftarkan di theme.json ​

json
{
  "version": 2,
  "customTemplates": [
    {
      "name": "emptycanvas",
      "postTypes": ["page"],
      "title": "Empty Canvas"
    }
  ],
  "settings": {
    // ... settings lainnya
  }
}
PropertyFungsi
nameNama file template (tanpa .html)
postTypesPost types yang bisa menggunakan template ini
titleJudul yang tampil di UI pemilih template

Langkah 3: Pilih Template per Halaman ​

  1. Buat/edit halaman di WordPress
  2. Di sidebar kanan → tab Page → bagian Template
  3. Klik → pilih "Empty Canvas"
  4. Update/publish

Cara Kerja ​

Halaman dengan template "Empty Canvas":
  ┌─────────────────────────────────┐
  │ (TANPA header)                  │
  │                                 │
  │ Konten yang ditulis user:       │
  │   - Banner block                │
  │   - Heading                     │
  │   - Events & Blogs              │
  │   - Slideshow                   │
  │   - dll.                        │
  │                                 │
  │ (TANPA footer)                  │
  └─────────────────────────────────┘

User bisa INSERT block apapun yang diinginkan:
  ✅ Custom blocks (banner, slideshow, header, footer)
  ✅ Core blocks (paragraph, heading, image)
  → Fleksibilitas TOTAL per halaman

Kekuatan Template Blank ​

  • User bisa menambahkan header block secara manual jika diinginkan
  • User bisa memilih block per halaman — berbeda dari template standar
  • Tidak ada batasan — user bisa membangun halaman dari nol
  • Cocok untuk landing pages, promo pages, atau halaman unik lainnya

Extra Credit: Extend Block Options ​

Ide pengembangan lebih lanjut:

  1. Header block → JSXBlock — tambahkan opsi toggle logo (show/hide via checkbox)
  2. Banner block — tambahkan dropdown untuk pilihan overlay color
  3. Per-page block restriction — batasi block berbeda per post type
php
// Contoh: Header block dengan opsi
new JSXBlock("header"); // Ubah dari PlaceholderBlock ke JSXBlock

// header.js
attributes: {
  showLogo: { type: "boolean", default: true }
}
// InspectorControls dengan ToggleControl untuk show/hide logo

Rangkuman Chapter 27 — Semua Fitur Block Theme ​

Block Types yang Dibuat ​

BlockTipeInteraksi
BannerJSXBlockBackground image, InnerBlocks
Generic HeadingJSXBlockRichText, size S/M/L
Generic ButtonJSXBlockRichText, link picker, color picker
SlideshowJSXBlockContainer untuk slides
SlideJSXBlockBackground image (= banner copy)
Events & BlogsPlaceholderBlockPHP only
HeaderPlaceholderBlockPHP only
FooterPlaceholderBlockPHP only
Single PostPlaceholderBlockPHP only
PagePlaceholderBlockPHP only
Blog IndexPlaceholderBlockPHP only
Program ArchivePlaceholderBlockPHP only
Single ProgramPlaceholderBlockPHP only
Single ProfessorPlaceholderBlockPHP only
My NotesPlaceholderBlockPHP only

Konsep Utama yang Dipelajari ​

NoKonsep
1Full Site Editing (FSE) & block theme structure
2JSX conversion (className, style objects)
3InnerBlocks & nested blocks
4RichText & BlockControls toolbar
5theme.json (colors, typography, layout)
6LinkControl & Popover
7InspectorControls & ColorPalette
8PHP render callback (no HTML in database)
9MediaUpload & apiFetch
10wp_localize_script (PHP → JS data transfer)
11PlaceholderBlock class
12Multiple slideshows fix
13Template system & themeimage attribute
14Block restrictions per environment
15Blank template for landing pages
16WP 6.4 singular content change