[Support request] Hook

Home Forums Support Hook

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1036698
    Gustav

    How do I add a hook in the middle of a blog post?

    #1036730
    David
    Staff
    Customer Support

    Hi there,

    can i ask what you want the hook for?

    #1036733
    Gustav

    For Adsense ad codes

    #1036742
    David
    Staff
    Customer Support
    #1036747
    Gustav

    Yeah. But the hook is far easier to customize when you want your ads to appear on a specific post,category or page. That’s why I prefer the hook to the plugin

    #1036765
    David
    Staff
    Customer Support

    Hooks don’t exist within the content.
    There is one method, that feels completely wrong, and that is to register a hook inside a shortcode – heres the PHP snippet to do that:

    function your_shortcode($atts, $content = null) {
          ob_start();
          do_action('your_hook_name');
          return ob_get_clean();
    }
    add_shortcode('shortcode_name', 'your_shortcode');

    Completely untested for what you require but in theory it should work.

    Note: you will want to rename the shortcode function / name and hook.

    #1038236
    Eric

    Hey David, thanks for the reply again…

    Sorry, I’m not too familiar with shortcodes, registering them in WordPress in particular… I placed the code into the Functions.php right? I tried replacing the name but I think I messed it up somehow?

    https://pasteboard.co/ICyz6zZ.png

    Could you give me a few steps as to how to complete this solution? So, I’m assuming I need to place this code into functions.php, then create another hook? How do I connect the two of them exactly?

    Thanks for the help and sorry about the inexperience.

    #1038253
    David
    Staff
    Customer Support

    E.g

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

    Then in your post you add the shortcode: [hooky_shortcode]

    And in Elements you would choose custom hook and add custom_shortcode_hook

    #1038263
    Eric

    It worked! Thank you David! Generatepress RULES… 🙂

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