Need help in fixing/debugging wordpress to know why my block is not visible in my theme’s block editor

I am new to WordPress development but have decent experience with Javascript.

So I’ve tried out this tutorial on Udemy (https://www.udemy.com/course/become-a-wordpress-developer-php-javascript/) and the project for the course worked out perfectly. So I decided to create a new theme from scratch and I seem to be stuck at the most basic step. Creating my first block. So it’s a very basic block with this file structure in my wp-content -> themes folder

my-theme-folder

src

header

block.json

index.js

render.php

functions.php

Here’s my block.json

{
  "$schema": "https://schemas.wp.org/trunk/block.json",
  "apiVersion": 3,
  "name": "my-theme/header",
  "category": "text",
  "title": "My Theme",
  "description": "The header for My Theme",
  "keywords": ["header"],
  "editorScript": "file:./index.js",
  "render": "file:./render.php"
}

Here’s my index.js

import { registerBlockType } from "@wordpress/blocks";
import metadata from "./block.json";

function Edit() {
  return <div>Hello from admin backend.</div>;
}

registerBlockType(metadata.name, {
  edit: Edit,
});


Here’s my render.php

<div>
    <h3>Hello from the front end </h3>
</div>

And finally here’s my functions.php

<?php

// ENQUEUE CSS AND JS

function theme_scripts(){
    wp_enqueue_script( 'theme-script', get_theme_file_uri('/build/index.js'), null,'1.0', true );
    wp_enqueue_style('theme-style',get_theme_file_uri('/build/style-index.css'));
    
}
add_action('wp_enqueue_scripts','theme_scripts');

// REGISTER BLOCK

function my_theme_blocks(){
    register_block_type_from_metadata(__DIR__ . '/build/header');
}

add_action('init','my_theme_blocks');

I am using this package.json

{
  "name": "vpawards",
  "version": "1.0.0",
  "description": "Theme for VP Awards ",
  "main": "index.js",
  "scripts": {
    "start": "wp-scripts start src/index.js",
    "blocks": "wp-scripts start --experimental-modules",
    "buildIndex": "wp-scripts build src/index.js",
    "buildBlocks": "wp-scripts build --experimental-modules",
    "build": "run-s buildIndex buildBlocks"
  },
  "author": "Alim Bolar",
  "license": "ISC",
  "dependencies": {
    "@wordpress/scripts": "^28.0.0",
    "npm-run-all": "^4.1.5"
  }
}

And I run npm start on one terminal and npm run blocks on another terminal as that’s what the teacher for the tutorial suggested as the ‘tried and tested’ method/workaround to ensure a smooth workflow and it does work.

Now I have a similar set up for the project that we completed at the course and I can’t seem to figure out why my code is not working (why my block is not visible in the block editor when I try to search for it) and I can’t seem to understand how to debug. There’s no ‘console.log’ option that I am used to with javascript and I’d really appreciate some help on fixing the problem or even guiding me on how to approach debugging in wordpress.

I have a thought about what the problem could be.. but I am not sure.. In the block.json the name property has this value – ‘my-theme/header’ … on what basis does wordpress know this is the right one and what if I use another namespace instead of ‘my-theme’ for another block in the same theme? I am assuming it would throw an error but I am not sure. The documentation says that it should correspond to the name of my theme but where and how do I register this namespace as the name of my theme?

Any advice would be appreciated.

Regards,
Alim

I’ve tried changing the namespace and then I added the project from my course as a theme as it was working and tried to remove some components to figure out at what stage does it start to give a problem but I couldn’t find anything specific. Finally when I tried to keep the bare minimum of the project’s course, even that was not working on my site. So it’s something that I am doing or not doing with my code. I’ve tried googling and here’s a link to a situation similar to mine and he seems to have found a workaround by totally avoiding the register block method which is not the way I would like to go..

Custom WordPress block not loading into editor using block.json, register_block_type

New contributor

Aly Sunshyn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật