🎉 Launch Party!

discount for all licences until Aug 31.

Buy now →

Register custom blocks

Create your own custom blocks in PHP and add customizations through fields groups.

With Modern Fields, you can register a custom WordPress block and assign a group of fields. Then, write your own PHP template. No JS, no React and no compilation required.

Custom blocks with fields are only available in the pro versions of Modern Fields.

Step 1: Register a new block

First, go to Modern Fields > Blocks. You’ll see the list of registered custom blocks.

Click on “Add a new block” button.

Fill in the fields:

Effect
TitleThe name of the block.
NamespaceLets you distinguish your blocks from others. Set the same namespace for all your blocks.
SlugUnique identifier for your block.
CategoryThe category in which the block will appear in the inserter.
IconIcon for the block.
KeywordsSet keywords to help
EnabledDisable the block without deleting it.

Then, select the supports to activate to your block. Supports are customization features for blocks that can be activated in one click.

Effect
AlignmentAllow your block to have wide / full width alignement
ColorAdd a color/gradient selector for text, background and link
SpacingAdd spacing ranges for margin, padding and block spacing
TypographyAdd font size and line height controls
DimensionsAdd the minimum height control
OthersOther options are available for your block. Check the WordPress documentation.

Prefer using native supports instead of fields for the features listed above.

Register a new block without code

Step 2: Assign a fields group

Now, you need to create a fields group and attach it to your custom blocks. Go to Modern Fields > Field groups and create one.

In the location selector (at the bottom of the screen) select Blocks > Custom Blocks.

Create a fields group and assign it to the custom block

You can also define in which tab you want your fields to appear:

  • Content: suited for content fields like text, textarea, richtext.
  • Settings: suited for select fields like select, multi-token, button groups.
  • Styles: suited for color, spacing, range, border fields.
  • Advanced: for ultra specific fields.

The last one is not a proper tab: fields will appear in the advanced panel at the bottom of the content tab, collapsed by default.

If you need to set fields in different tabs, you’ll need to create several groups and assign them to the same block, different tab.

Note: The fields will be save in the blocks attributes, and not in the post meta table.

Step 3: Create the template

In the last step, you need to create the template file in your theme, more specifically in the /mf-blocks/ folder.

First, get the name of your block from the block registration screen.

If you block name is mf/custom-block, the file name is mf-custom-block.php.

Now, write your template in PHP. The field values are exposed through the $field array.

PHP
/wp-content/themes/my-theme/mf-blocks/mf-custom-block.php

Don’t forget to add get_block_wrapper_attributes() in the first tag to enable supports.

Write the block template in PHP

Your block should be available in the block editor.

If you can’t see it, go back to Modern Fields > Blocks and check everything is green in the block checklist.

Register a stylesheet for the block

Now, if you need some custom styles for your block, you can use the native wp_enqueue_block_style() function.

PHP
functions.php

In this example, the CSS file is located in the assets/css/ folder.

Change the folder name

If you need to change the custom blocks templates folder name, use this filter in your functions.php file.

PHP
functions.php

Define the name in this filter and create the corresponding folder in your theme.