Site logo

[Support request] Block element inside another block element content

Home Forums Support [Support request] Block element inside another block element content

Home Forums Support Block element inside another block element content

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #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,
    Dasha

    #1904275
    David
    Staff
    Customer Support

    Hi 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_hook

    2. 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_hook to 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

    #2470666
    martijn

    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,
    Martijn

    #2470709
    David
    Staff
    Customer Support

    Hi 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.

    #2470719
    martijn

    David,
    Now I get it.
    Thank you so much for your fast and excellent support!

    Regards,
    Martijn

    #2471367
    David
    Staff
    Customer Support

    You’re welcome

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.