Site logo

Archived topic

Hook

10 replies · Started by Gustav on October 16, 2019

Viewing posts 1–11 of 11

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

Hi there,

can i ask what you want the hook for?

For Adsense ad codes

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

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.

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.

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

It worked! Thank you David! Generatepress RULES... :-)

Hey David, I also want to add a hook inside the content. I check the codes you gave but I didn't understand it.
can you please help me understand this?

Hi Ravi,

Can you open up a new topic for your concern?

We'll make sure to address your concern there. Thank you. :)

This archived topic is closed to new replies.