• Resolved capbussat

    (@capbussat)


    I want to output some html as a floating language selector only in the pages that contain my plugin block. But, If I use the block twice in the template , the selector is output twice (In the same place).

    I wonder if there is some easy way to check if the block plugin is present in the page (template) but output the html content only once!

    Any action or filter to check when all the blocks are shown in the page?

    Thanks

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Andry

    (@blackstar1991)

    You need to keep track of the unique identifier for a given block, and then before outputting content do a check i if there is more than one block on the page return return for a particular block.

    Moderator bcworkz

    (@bcworkz)

    You can use has_block() to check if a specific block exists on a page, but depending on what you use to trigger the check, you could still end up with multiple insertions. Some action hooks will fire multiple times for no apparent reason. One way to ensure a callback only executes once is to have it remove itself from the action stack. Do so on initial entry. Waiting until the end could cause a race condition where your callback is called again before the removal can be completed.

    Thread Starter capbussat

    (@capbussat)

    Thanks, I think for me its easy to use has_block. I have found out there is also an action hook called ‘wp_body_open’ (which exists since wp 5.2) and seems very appropiate to output html code. So combining both I have tested it, so far seems to output only once with Twenty Twenty Three theme.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Render an HTML banner from a block’ is closed to new replies.