Repository navigation
Conversation
This addition attempts to explain how to create a dynamic block that takes user input in the editor (in this example in the form of a rich text input) and then uses PHP to output this on the front-end. The benefit here is that the blocks markup can change without needing to update each of the posts using that block. I found this page lacking in how to do this as it focused on dynamic blocks that carry out a query in PHP for the content rather than output the takes user input from the block itself. Hopefully it will help others.
tofumatt
left a comment
There was a problem hiding this comment.
I don't think this is the right messaging for dynamic blocks, but the example of how to write a dynamic block with both JS and PHP code for reference is cool. 😄
I think this would be better refactored with just the example and not the implication/suggestion to developers to use dynamic blocks for markup management.
|
|
||
| ## Rendering block markup with PHP | ||
|
|
||
| Dynamic blocks can be used in order to control the markup of your block with PHP. This is useful as if you decide to change your blocks markup, you won't have to go back and re-save all the posts which use that block, in order for your blocks markup change to be reflected on all posts. |
There was a problem hiding this comment.
There are some issues here with grammar (blocks markup should be block’s markup).
But beyond that, I don't think we should be promoting dynamic blocks in this way… dynamic blocks exist as a way to render content that requires interaction with the server, but not as a way to not have to worry about changing block markup or avoiding dealing with deprecating block markup.
This write-up makes it sound like the purpose of dynamic blocks is to make it easy for block developers to change their markup without effort… but that's not their purpose 😄
That approach can be problematic for users too because their content will change without them expecting it when a block is updated.
There was a problem hiding this comment.
@tofumatt thanks for the feedback - grammar is not my strong point.
The wording here doesn't insist that dynamic blocks can be used for controlling block markup, only that they can be used in this way.
Dynamic blocks can be used in order to control the markup of your block with PHP
The key word here is 'can'.
If dynamic blocks shouldn't be used to for controlling block markup, what can please? Saving the markup in the the_content means that the markup cannot be changed or updated without the post being re-saved. Obviously, there must be another method for doing this and I have got mixed up between the two.
There was a problem hiding this comment.
No worries about the grammar! 😄
I suppose if you wanted to centrally control markup you could use dynamic blocks, but my question would be why you'd want to control the markup itself. I can see using dynamic blocks for things like Latest Posts because the client can't render them, but the client can render HTML just fine and keeping content local improves the editing experience.
That said, there's (currently) no method to deal with updating block content en masse if it's not dynamic.
My worry is that the docs, as worded, will push developers toward dynamic blocks for ease of creation at the cost of user experience editing content. Dynamic blocks can't re-render offline, will be slower because of network latency, etc. Maybe I'm being a bit too cautious but I know there's a temptation to use dynamic blocks for everything to avoid much JS 😅
There was a problem hiding this comment.
If the current implementation means that markup cannot be changed without updating all posts that use a block then this is a massive problem. Imagine you make a block for a multi-site install of WordPress (we have 2 with over 500 sites on each) and realise that you need to change the markup for whatever reason - it happens more than you think. For example, if the design of the site changes and an additional element is needed to wrap, go before or after content to meet the needs of the design.
Each site might have hundreds of posts, pages etc. using that block and therefore a way to do this without saving the block on all posts is a must here.
Maybe this is a seperate issue that needs addressing?
There was a problem hiding this comment.
There was a problem hiding this comment.
Umm. In #4849 @youknowriad says:
Yes, dynamic blocks are the way to go in this case.
Blocks markup to block's markup.
| function my_render_dynamic_block( $attributes, $content ) { | ||
|
|
||
| // we can now return the rich text fields content with whatever markup we like. | ||
| return '<div class="my-richtext-block">' . $content . '</div>'; |
There was a problem hiding this comment.
As I said in #4849 (comment) I don't think we should add this to our docs because it recommends to people to use dynamic blocks for static ones.
|
@chrisvanpatten, @ajitbohra, @mkaz and @nosolosw - given that we have now several tutorials, how does it fit to the picture? Should we invest more time to apply those changes or is it fine to close it? |
|
@gziolo we already have a tutorial covering the creation of dynamic blocks using ServerSideRender Current tutorial address the concern raised by @youknowriad as we are recommending to use it as a fallback.
Seems like its fine to close this. |
|
Agreed, we can close this one, as @ajitbohra commented the block tutorial covers dynamic blocks |
|
@wpmark thank for your contribution. As discussed above docs has been improved a lot in the recent weeks based on the feedback shared by the community. There is a common agreement amongst those who are involved in all docs efforts the most that the changes you proposed are already covered in other places. I'm closing this PR. Feel free to open a new PR if you want to target some specific aspects of docs. |
This addition attempts to explain how to create a dynamic block that takes user input in the editor (in this example in the form of a rich text input) and then uses PHP to output this on the front-end. The benefit here is that the blocks markup can change without needing to update each of the posts using that block.
I found this page lacking in how to do this as it focused on dynamic blocks that carry out a query in PHP for the content rather than output the takes user input from the block itself. Hopefully it will help others.
Description
How has this been tested?
Screenshots
Types of changes
Checklist: