Site logo

Archived topic

Creating a shortcode to show a block repetitively using dynamic parameters

5 replies · Started by Eyal on August 4, 2022

Viewing posts 1–6 of 6

I need to create a shortcode that will retrieve several comments from the database and render them using a block.
I know how to write the php, but I do not find a way to:
- Create a block with the desired design
- Put placeholders for dynamic content
- Way to replace this content with the data I retrieve from the database

The easy way was to be able to create a block with design and placeholders, and then I will put code in functions.php that will retrieve comments from the database, and render the block + parameters, then I send all via the shortcode output.

Please advise how can I do it.

Hi Eyal,

In your Shortcode, you can echo out html. Add classes to your elements so you can modify them with CSS.

You can place a place holder in your HTML as well, and then use str_replace to replace the placeholder if you are able to retrieve data successfully: https://www.php.net/manual/en/function.str-replace.php

You can also use function get_comments() to retrieve comments in WordPress. Example I found: https://stackoverflow.com/questions/39671448/wordpress-get-recent-comments#:~:text=You-,can%20retrieve,-recent%20comments%20using

Hi,
The way you described is the common way to use shortcodes with HTML/CSS etc - I am familiar with all these :-)
But what I try to achieve is using the GB block that designed in the Wordpress as the html template, not build the html by myself.
Is there any way to do that?

Yes, there is. You can try render_block filter. With this filter, you can alter the outpout of a Block.

Reference: https://developer.wordpress.org/reference/functions/render_block/

Example for block with class my-placeholder:

add_filter( 'render_block', function( $block_content, $block ) {
    if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-placeholder' ) !== false ) {
		
        //enter code here
		
    }

    return $block_content;
}, 10, 2 );

If I understand you correctly, this filter "catches" block rendering activity (since it's a filter) and then in code I should identify if that's the relevant block (according to class name) and do something.
But this does not help me using specific block repetitively with the html that was produced by the GB (when I created/designed it in the page).
I need to build a block once (e.g. "display comment" block) and be able to generate it for each and every comment that I read from the database using the "get recent comments" you mentioned above.

Hi there,

we just added Query Loop to the GenerateBlocks plugin, in future updates we will be looking at other types of data / arrays that it can loop through including things like Comments.

But today that is not possible, and developing a custom block solution is not something we can assist with in this forum, you would need to custom develop that.

This archived topic is closed to new replies.