Skip to content

PoC: bare function vs class method - #236

Draft
matt-bernhardt wants to merge 1 commit into
lnhu-225from
lnhu-225-alt-a
Draft

matt-bernhardt wants to merge 1 commit into
lnhu-225from
lnhu-225-alt-a

Conversation

@matt-bernhardt

Copy link
Copy Markdown
Member

This demonstrates the alternate way to implement a register_blocks() call, changing from a bare function (directly in the plugin's root PHP script) to a method in the class file at src/class-heroimage.php.

There are three conceptual changes here:

  1. We move register_blocks() from a function in the root PHP script to a method in the PHP class.
  2. We have to update the arguments that are passed to the "register_block_types" function, from the __DIR__ . '/build' pattern to plugin_dir_path( __DIR__ ) . 'build'. This is necessary because the method is one level deeper in the plugin, so we can't just "go down one level into the "build" folder" because now the code lives in the src/ folder. There is an even more robust pattern we could use that would rely on a constant defined in the PHP script, but that really only becomes necessary if our plugins start getting into multi-level folder structures.
  3. The last change is to move the add_action() call up to the rest of them, and update the instructions for where to find the thing being called - from Mitlib\PostTypes\register_blocks to `array( 'Mitlib\PostTypes\HeroImage', 'register_blocks' )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant