Support

Account

Home Forums Gutenberg Change _underscore names to -dash names without loosing content

Solving

Change _underscore names to -dash names without loosing content

  • I have always named all group, blocks, fields etc in this scheme in past projects:

    block_table

    Now for the first time I registered a block with block.json and found that the underscore causes trouble with for example

    allowed_block_types_all

    which works fine if you write the name like this:

    block-table

    My question is now: How can I change the names to dash instead of underscore in my past projects in such a way that I do not lose the content of all blocks.

    Am grateful for any tip!

  • Changing field names is difficult. Doing this when using the block editor is even more difficult.

    What is involved is finding and changing the field name in the database for every occurrence of the field. Every field has 2 entries, the first is the field name and the second is the field key reference which is the field name prepended with an underscore. Without the block editor this information is stored in the various meta tables. Using the block editor this information is stored in post content.

    Basically, once a field is in use you just do not go back and change the field name, you live with those past decisions.

  • Thanks for your answer, John. That sounds really difficult and also prone to errors.

    The reason for the change would have been that I would have liked to switch the older projects from registering blocks with PHP, to those via block.json. There the problem with the underscores already starts.

    One can only advise everyone to avoid underscores. I had started this way, because it was written here: https://www.advancedcustomfields.com/blog/best-practices-designing-custom-fields/#1-naming-your-fields-

    It is to be hoped that the registration of blocks by PHP remains downward compatible and WP does not throw this option out sometime.

    I hope I’m not confusing anything 😉

  • I can’t tell you how to name blocks, I don’t use them. If it makes more sense to name blocks with a dash than an underscore then I would just use dashes in the future. I don’t see how this would be affected by a field name unless you are dynamically naming your blocks based on the field name.

    Using underscores in really still the best practice for field names. The reason for this is that there are places where arrays are extracted to variables. How often this happens and were I’m not sure. But array indexes with dashes cannot be extracted.

    Quite honestly, not knowing much about blocks, I would say that it has something to do with how WP is parsing the block names when looking at “allowed_block_types_all” You might want to look at this filter: allowed_block_types_all.

  • Thank you for your detailed explanation.

    Let me summarize how I came up with that. I use

    allowed_block_types_all

    for whitelisting blocks, which also excluded my ACF blocks… which is obvious.

    I’m using the “Basic Example” from here

    For whatever reason

    'acf/comparison_table'

    did not work for unblocking it in my test blog, but

    'acf/comparison-table'

    did.

    I changed it in the block.json

    "name": "acf/comparison-table",

    and in the whitelist, of course.

    No idea what causes this, but maybe this will help someone. I have been looking for the cause for a while.

    It’s probably how WP is parsing the block names, as you say.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.