Site logo

Archived topic

Place an element through shortcode

7 replies · Started by Daniele on August 17, 2021

Viewing posts 1–8 of 8

Hi, I have placed a featured image in the categories, and I would like to insert an element with buttons right under that image, but I have tried many ways, I have not been able to place the element at that point. So I thought about using a shortcode to insert the element where I want. But is it possible to have shortcodes for elements?

Thanks!

Daniele

Hi Daniele,

So I thought about using a shortcode to insert the element where I want. But is it possible to have shortcodes for elements?

That's definitely possible.

I'm not exactly sure which element are you pertaining to but if it's a Header element, its code area can run shortcodes.

If it's a Hook element, you'll have to tick the "Execute Shortcode" to make shortcodes run on its code area.

If it's a Block element, you'll have to add a "Shortcode Block" and add your shortcode in it. :D

Hi Elvin,
We did not understand each other. I already know that I can put shortcode inside an element. What I want to do instead, is insert an element using a shortcode :) How can I do?

Thanks,
Daniele

ah ok, that code goes into the function file, so I'll need a child theme, right?

Nevermind, solution already found after 2 hours of investigation. I've shared my solution below in bold.

I was also looking for this. I want to use "Elements" function to use an Element as "Global content" on certain pages between other content. I can't use any other hooks for this because it's between other content.

David's code makes it neccessary to, every time I want to create new "Global content" by adding an Element and inserting via shortcode, I need to change the functions.php. Because I need other users to add new global elements themselves, I need a solution where I don't need to change the php code for each new element.

But for me, both options do not work.

I've added David's code to my child theme's function.php file:

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');

Unfortunately, when adding the shortcode [hooky_shortcode] inside my page content, the Element content was not inserted.

When this did not work, I tried the more flexible "Insert PHP" option and hoped this would also work for a Block element. So I tried the following:

I've added this to the Child Theme's functions.php:

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();
});

I've added an Element with the following settings:

  • Element type = Block (in the Create element popup)
  • Hook type = Hook
  • Hook name = Custom hook
  • Custom hook name = globaal_normen

I've used the following shortcode to insert the Element's in between other content:
[portable_hook hook_name="globaal_normen"]

Unfortunately, this doesn't display the Element's content at the place of the shortcode.

I also don't understand if I need to do something with the text 'no foo' in the php code.

Some remarks:
- In both scenarios I've used the Gutenberg 'shortcode' block and I am using GenerateBlocks Premium.
- I don't want to use the GenerateBlocks Templates feature because I want this to be "Global content" which I only want to change in the element
- I prefer the 'Flexible' option

Solution: I - of course - also needed to set the Display: Entire site function at the bottom of the Element, which I've forgotten..

@Patrick,

Thanks for letting us know. Glad you got it sorted. :D

This archived topic is closed to new replies.