Site logo

[Support request] Place an element through shortcode

Home Forums Support [Support request] Place an element through shortcode

Home Forums Support Place an element through shortcode

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

    #1899615
    Elvin
    Staff
    Customer Support

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

    #1900657
    Daniele

    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

    #1900694
    David
    Staff
    Customer Support
    #1904684
    Daniele

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

    #1904858
    Elvin
    Staff
    Customer Support

    ah 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-1713422

    It’s a portable hook you can place anywhere.

    #1980075
    Patrick

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

    #1981464
    Elvin
    Staff
    Customer Support

    @Patrick,

    Thanks for letting us know. Glad you got it sorted. 😀

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