π¦ NEW: Providing warning on invalid block name β FIX #28#40
Merged
ahmadawais merged 3 commits intoMar 25, 2018
Conversation
Adding regex checking whether block name is valid or not and a function that logs error message
ahmadawais
reviewed
Mar 23, 2018
Member
ahmadawais
left a comment
There was a problem hiding this comment.
Looks good to me. Just need to test it. Thanks for the PR. I hope to get more help from you. Welcome as a contributor to this project. πππ
Author
|
Pleasure is all mine and I'm glad I was able to put my 2 cents into this cool project. Will do my best to contribute more often ππ |
Member
|
@wpjsio ok, tested, it all looks good. Thanks a lot. I welcome more contributions from your end, take a look at ahmadawais/create-guten-block#33 Roadmap for 2.0. Also, I'd appreciate if you keep the git commit log in line with https://gist.github.com/ahmadawais/815ea20134cf4616a6b2a965eb08f716 Merged. Thank You! π Peace! βοΈ |
This was referenced May 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR tries to approach issue #28.
To be on the safe side and cover all possible scenarios I started with checking where the user provided
blockNameis used and what are the limitations of each case:Gutenberg's
registerBlockTypefunctionnameargument:'Block names must (...) include only lowercase alphanumeric characters or dashes,
and start with a letter.'
wp_enqueue_scripthandle: - it simply has to be a non-empty stringGutenberg's block title - it has to be a non-empty string too
Suffix for
.wp-block-cgb-block-${blockName}CSS class:Now, when we sum up all the above we can come to a conclusion that:
blockName's first character has to be a letter.blockNamecan contain spaces (ie: 'my awesome block') as they will be replaced by hyphens.blockNamecan contain uppercase letters as it will be converted to lowercase anyway.blockNameshouldn't contain underscores(_) as they are valid in php, but will cause errors when we are trying to register our blockblockNamecan contain dashes (-) [thanks to:<% blockNamePHPLower %>]Having this in mind I think that following regex should suffice to do the job:
/^[a-z][a-z0-9-]/As I mentioned spaces and uppercase letters are filtered anyway, so I think it's better to put the code after the formatting
blockNameinstead of adding it into regex.Wow, quite a long output for such a short regex, but hopefully it's good enough.
Cheers π»