[Support request] hook in hook

Home Forums Support [Support request] hook in hook

Home Forums Support hook in hook

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1635107
    detanatar

    is it possible to insert one hook into the content of the second hook?

    #1635134
    Elvin
    Staff
    Customer Support

    Hi there,

    I’m not sure I fully understand what you mean.

    Can you explain a bit more? Perhaps be more specific in what you’re trying to do? Let us know. 🙂

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

    #1636030
    detanatar

    there is a block “A” created by the hook,
    this block is shown on pages 1, 2, 3, 4 …
    inside block “A” block “B” is shown
    Block “B” is created by hooks, and it is different for each page.
    That is, there are hooks “B1”, “B2”, “B3”, “B4” ..

    before, for each page I created a summary block “AB1”, “AB2”, “AB3”, “AB4” …

    Block “B” has little code, and I was planning to set up impressions in my pages for every “B” block, and insert links to blocks “B” in block “A”

    #1636333
    Elvin
    Staff
    Customer Support

    Consider this:

    You can actually make a portable hook you can place anywhere.

    Here’s an example creating a portable hook through a shortcode.

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

    What this particular shortcode does is, it adds a hook anywhere you place this shortcode.

    to use this, write shortcode in this manner [portable_hook hook_name="my_custom_hook"] where my_custom_hook is the hook name.

    Try placing it anywhere in your site where shortcodes work.

    Now to test it, you can use add_action() using my_custom_hook.

    Say for example, you to hook something inside this hook, you can do it the same way you add_action to other hooks.

    Example: add_action('my_custom_hook', function(){ echo 'added in portable hook shortcode'; });

    We can also place this portable inside a hook.

    Say for example, you want to place a hook named special_hook on another hook named generate_after_header.

    You can do it by using this:

    add_action('generate_after_header',function(){ 
         do_shortcode('[portable_hook hook_name="special_hook"]'); 
    });

    This shortcode was suggested so you can place your custom hooks within the content area.

    But if you don’t want to bother with that and simply place a hook area within any existing hooks within the theme, then you can completely skip creating a shortcode and simply do something like this:

    Example: placing another_hook hook within GeneratePress’ generate_before_content hook

    add_action('generate_after_header',function(){ 
         do_action('another_hook'); 
    });

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

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