Skip to content

Chapter 3: WordPress Specific PHP — Convert Static HTML Template into WordPress (Part 1 & 2)

Ringkasan

Dua video ini membahas cara mengubah halaman HTML/CSS statis menjadi WordPress theme yang hidup. Kita akan meng-copy HTML dari template statis ke file-file theme WordPress, memindahkan folder aset (CSS, images, JS), dan memuat file-file tersebut melalui functions.php.


Konsep Utama

  • Kita tidak menulis CSS dari nol — kita menggunakan template HTML/CSS yang sudah jadi
  • Tugas kita: mengintegrasikan HTML statis ke dalam theme WordPress, lalu mengganti konten hardcoded dengan data dinamis dari WordPress

Step-by-Step

Step 1: Download Template Statis

  1. Kunjungi: github.com/learnwebcode/university-static
  2. Klik Download ZIP → Extract
  3. Buka file index.html di browser untuk preview

Template ini 100% statis — tidak ada PHP, tidak terhubung ke WordPress. Link-nya tidak berfungsi.

Step 2: Copy Header HTML ke Theme

  1. Buka index.html (file yang didownload) di text editor
  2. Cari elemen <header> di bawah <body>
  3. Copy seluruh elemen <header>...</header>
  4. Buka header.php di folder theme WordPress
  5. Paste di bawah tag <body>, gantikan headline placeholder

📄 header.php — setelah diubah:

html
<!DOCTYPE html>
<html>
  <head>
    <?php wp_head(); ?>
  </head>
  <body>
    <header>
      <!-- ... semua HTML header dari template statis ... -->
    </header>
  1. Di index.html, scroll ke bawah, cari elemen <footer>...</footer>
  2. Copy seluruh elemen footer
  3. Buka footer.php di folder theme
  4. Paste menggantikan dummy paragraph, tetapi jaga wp_footer(), </body>, dan </html>

📄 footer.php — setelah diubah:

html
    <footer>
      <!-- ... semua HTML footer dari template statis ... -->
    </footer>

    <?php wp_footer(); ?>
  </body>
</html>

Step 4: Pindahkan Folder Aset ke Theme

Dari folder template yang didownload, pindahkan 4 folder ke dalam folder theme WordPress:

📁 fictional-university-theme/
  ├── 📁 build/       ← ⭐ CSS & JS yang sudah di-compile
  ├── 📁 css/         ← ⭐ CSS tambahan
  ├── 📁 images/      ← ⭐ Gambar/foto
  ├── 📁 src/         ← ⭐ Source files
  ├── 📄 index.php
  ├── 📄 header.php
  ├── 📄 footer.php
  ├── 📄 functions.php
  ├── 📄 style.css
  └── ...

Step 5: Update functions.php — Load CSS Files

Ubah functions.php untuk memuat CSS dari folder build/:

📄 functions.php — versi baru:

php
<?php

function university_files() {
    wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
    wp_enqueue_style('university_extra_styles', get_theme_file_uri('/build/index.css'));
    wp_enqueue_style('font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
    wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css?family=Roboto+Condensed:300,300i,400,400i,700,700i|Roboto:100,300,400,400i,700,700i');
}

add_action('wp_enqueue_scripts', 'university_files');

Penjelasan setiap wp_enqueue_style():

#NicknameFile/URLTujuan
1university_main_styles/build/style-index.cssCSS utama layout & komponen
2university_extra_styles/build/index.cssCSS tambahan
3font-awesomeURL eksternal CDNIcon library (untuk ikon sosmed di footer, dll)
4custom-google-fontsURL eksternal Google FontsCustom font (Roboto)

Fungsi baru yang diperkenalkan:

FungsiKegunaan
get_theme_file_uri('/path/to/file')Mengembalikan URL absolut ke file di dalam folder theme. Lebih fleksibel dari get_stylesheet_uri() karena bisa menunjuk ke subfolder/file apapun

Catatan style.css: Kita tidak lagi memuat style.css untuk styling (isinya cuma comment info theme). CSS sebenarnya ada di folder build/.

Step 6: Load JavaScript File

Tambahkan di dalam function university_files():

php
function university_files() {
    wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);

    wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
    wp_enqueue_style('university_extra_styles', get_theme_file_uri('/build/index.css'));
    wp_enqueue_style('font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
    wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css?family=Roboto+Condensed:300,300i,400,400i,700,700i|Roboto:100,300,400,400i,700,700i');
}

Penjelasan wp_enqueue_script() — 5 argumen:

php
wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);
//                  ↑ nickname            ↑ lokasi file                          ↑ dependencies   ↑ versi ↑ load di footer?
ArgumenNilaiPenjelasan
1 — Nickname'main-university-js'Nama bebas untuk script ini
2 — Lokasiget_theme_file_uri('/build/index.js')URL ke file JS
3 — Dependenciesarray('jquery')Script lain yang harus dimuat sebelum script ini. Gunakan null jika tidak ada dependency
4 — Version'1.0'Nomor versi script
5 — In Footer?truetrue = load sebelum </body> (lebih baik untuk performa). false = load di <head>

Step 7: Copy Konten Homepage ke index.php

  1. Dari index.html template statis, copy semua HTML antara </header> dan <footer>
  2. Di index.php, hapus loop code lama, paste HTML statis

📄 index.php — struktur baru:

php
<?php get_header(); ?>

<!-- paste semua HTML homepage dari template statis di sini -->
<!-- (page banner, welcome area, slideshow, blog section, dll) -->

<?php get_footer(); ?>

Step 8: Fix Path Gambar dengan get_theme_file_uri()

Gambar di template statis pakai path relatif (images/library-hero.jpeg), tapi di WordPress path-nya berbeda.

Sebelum (tidak berfungsi):

html
<div style="background-image: url(images/library-hero.jpeg)">

Sesudah (berfungsi):

html
<div style="background-image: url(<?php echo get_theme_file_uri('/images/library-hero.jpeg'); ?>)">
FungsiKegunaan
get_theme_file_uri('/images/library-hero.jpeg')Menghasilkan URL lengkap ke file di folder theme, contoh: http://fictionaluniversity.local/wp-content/themes/fictional-university-theme/images/library-hero.jpeg

Lakukan ini untuk SEMUA gambar di template — termasuk gambar slideshow di bagian bawah homepage.


Hasil Akhir

Setelah semua step selesai:

  • Homepage memiliki design yang lengkap (header, konten, slideshow, footer)
  • CSS dan font sudah ter-load
  • Icon Font Awesome muncul di footer
  • Slideshow berjalan berkat JavaScript yang dimuat
  • Gambar tampil dengan path yang benar

Poin Penting

  • get_theme_file_uri() adalah fungsi kunci untuk mereferensikan file apa pun di dalam folder theme
  • wp_enqueue_style() untuk CSS, wp_enqueue_script() untuk JavaScript — jangan load manual pakai tag <link> atau <script>
  • Gunakan wp_head() dan wp_footer() agar WordPress bisa inject aset secara otomatis
  • Memindahkan template statis ke WordPress pada dasarnya adalah: copy HTML → paste ke template files → ganti hardcoded content dengan fungsi PHP WordPress