[Support request] Re-using GenerateBlocks containers with modification by code

Home Forums Support [Support request] Re-using GenerateBlocks containers with modification by code

Home Forums Support Re-using GenerateBlocks containers with modification by code

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
  • #1579017

    I have GP Premium and GenerateBlocks. I have a GB Container that contains an custom HTML code with an iframe that I am using across multiple pages on the site, but I need to slightly modify a few things within the container based on which page I am on.

    So I converted the Container to a reusable block.

    I need to then make changes to the iframe.src.

    Is that best to do in one of these ways:
    1. on each separate page, where frame .src is modified by javascript?
    2. somewhere in the Elements module with a hook?
    3. in the separate Code Snippets module with custom php?
    4. some better way…

    Thanks for the guidance!

    To make the changes on each of about 30 pages,

    Customer Support


    Here’s another approach:

    Make a shortcode of mentioned iframe which checks for page conditions:

    Say for example: (shortcode that displays different iframe attributes on different specified pages. else do something else)

    add_shortcode( 'my_custom_iframe', function() {
        // Start your PHP below
      if( is_page( array('home','about','contact') ) ){
             echo '<iframe [attributes here> </iframe>';
       } else {
             echo 'place something else here';
        // End your PHP above
        return ob_get_clean();
    } );

    You then use [my_custom_iframe] within GenerateBlocks that is a reusable container.

    This way you don’t have to do anything within the Gutenberg Editor except for the layout and do everything within the shortcode’s snippet. ๐Ÿ™‚

    As for adding PHP, check here: https://docs.generatepress.com/article/adding-php/

    A wise man once said:
    "Have you cleared your cache?"


    Hmm…thank you, but is there a way to do this without using short-code? Maybe using javascript in Elements or PHP in Code Snippets for example?

    From a code management standpoint, I already some javascript stored in Elements Hooks, some PHP in Code Snippets, some javascript directly in Widgets and Pages in Custom HTML blocks, etc, so it is getting very complex to manage all of it.


    Can this kind of shortcode be added to Elements Hooks?

    I tried placing it in Elements, with both Execute PHP and Execute Shortcode checked, but if just echoes the text “add_shortcode( ‘my_custom_iframe….” etc to the screen

    Also tried putting it in the Code Snippets plugin but that didn’t work either (which I guess is what is being suggested in https://docs.generatepress.com/article/creating-a-shortcode/ ). I wish your documentation was a little more clear about what goes where where code examples are being shared.

    Customer Support

    Hi there,

    the shortcode snippet should go in the Code Snippets plugin.

    Once that is in place you can simply edit your Reusable block, insert a Shortcode Block and add: [my_custom_iframe]

    However, where is this re-usable block being placed ? Is it inside the post content ? Or could it be placed before or after the content ?


    Thanks, David. Yes, I’m placing the reusable block in the page content…at the top of the page content in fact.

    I was able to just put javascript into the Reusable Block right below the iframe. The javascript uses the location.pathname of the Page to then modify the iframe.src. I then put the reusable block across a dozen or so different Pages.

    It seems to be working fine and showing the correct iframe content on each page, so I guess this is ok of a solution unless you see some problem with using javascript in a reusable block like this? Thanks!

    Customer Support

    Sounds like a good solution ๐Ÿ™‚ And i can’t see there being any issues with adding JS in a block like that.

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