- This topic has 7 replies, 4 voices, and was last updated 4 years, 5 months ago by
Elvin.
-
AuthorPosts
-
August 17, 2021 at 7:25 pm #1899512
Daniele
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
August 17, 2021 at 10:26 pm #1899615Elvin
StaffCustomer SupportHi 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. 😀
August 18, 2021 at 2:39 pm #1900657Daniele
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,
DanieleAugust 18, 2021 at 3:58 pm #1900694David
StaffCustomer SupportAugust 22, 2021 at 1:09 pm #1904684Daniele
ah ok, that code goes into the function file, so I’ll need a child theme, right?
August 22, 2021 at 8:21 pm #1904858Elvin
StaffCustomer Supportah ok, that code goes into the function file, so I’ll need a child theme, right?
Not necessarily. You can use Code snippets plugin if you wish to avoid adding a child theme.
https://wordpress.org/plugins/code-snippets/David’s code works but here’s a more flexible version of his code.
https://generatepress.com/forums/topic/inserting-custom-php-html-into-gutenberg-or-site-container/#post-1713422It’s a portable hook you can place anywhere.
October 28, 2021 at 3:36 am #1980075Patrick
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’ optionSolution: I – of course – also needed to set the Display: Entire site function at the bottom of the Element, which I’ve forgotten..
October 28, 2021 at 5:02 pm #1981464Elvin
StaffCustomer SupportThanks for letting us know. Glad you got it sorted. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.