- This topic has 5 replies, 3 voices, and was last updated 3 years, 2 months ago by
David.
-
AuthorPosts
-
August 22, 2021 at 2:33 am #1904163
Dasha
Hello,
Is it possible to insert a block element inside another block element’s content?
For example, I’d like to make separate block elements for 1) newsletter signup 2) testimonials.
Then, I’d like to insert these block elements at a specific place inside another block elements content. Is that possible to do?Or what’s the best way to re-use small block elements to be able to insert them into a specific place inside other block elements?
Hope that makes sense.
Thank you,
DashaAugust 22, 2021 at 5:13 am #1904275David
StaffCustomer SupportHi there,
out of the box no – but you can do this, which feels really kinda hacky but actually works.
1. Create a custom shortcode by adding this PHP Snippet to your site:
function db_hook_inside_shortcode($atts, $content = null) { ob_start(); do_action('custom_shortcode_hook'); return ob_get_clean(); } add_shortcode('hooky_shortcode', 'db_hook_inside_shortcode');How to add PHP: https://docs.generatepress.com/article/adding-php/
This will create a shortcode called:
[hooky_shortcode]that has a Hook inside it named:custom_shortcode_hook2. Edit the Block Element and add the shortcode where you want the other Block element to be displayed.
3. Create your Block Element you want inserted in there, and set its Hook to Custom Hook and add
custom_shortcode_hookto the field provided.I don’t recommend you create multiple unique shortcodes and start getting all inception with them by putting them inside each other lol
December 22, 2022 at 8:33 am #2470666martijn
Hi there,
This “kinda hacky” solution works great. My issue is that I have three block elements placed in the content on certain pages. I’ve altered the code for the second and third element,so I’m using three similar snippets, but is this the best way to solve this?
Thank you,
MartijnDecember 22, 2022 at 9:01 am #2470709David
StaffCustomer SupportHi there,
you could use this snippet to create a shortcode:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook_name']); return ob_get_clean(); });Then you can add shortcodes with a arg for the hook name eg.
[portable_hook hook_name="your_custom_hook_name"]Add as many shortcodes each with a different hook name to create multiple hooks.
December 22, 2022 at 9:10 am #2470719martijn
David,
Now I get it.
Thank you so much for your fast and excellent support!Regards,
MartijnDecember 23, 2022 at 3:50 am #2471367David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.