Site logo

Archived topic

hooks related question

10 replies · Started by iamarghya on March 28, 2022

Viewing posts 1–11 of 11

I just read your hooks visual guide so I have a question... is there any way to hook an element before a certain paragraph inside the main content. Like I want to use some hook element that I want to place before a certain paragraph or photo is there any way?

Hi Iamarghya,

If you want to place an Element somewhere specific not in the list, you may create your own shortcode which create a new custom hook:

Portable hook code:

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

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Adding it through Code Snippets should work.

After adding this PHP, you may add the shortcode as such: https://share.getcloudapp.com/ApuJGooG

Then you can now hook to hook_name through custom hook.

Example: https://share.getcloudapp.com/eDu5xKLX

Hope this clarifies. :)

Can u just repeat the step once more, please , couldn't understand

Step 1

Download the plugin Code Snippets through Plugins > Add new: https://share.getcloudapp.com/P8u1mKJg

Step 2

Add this PHP code through Snippets > Add new:

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

Step 3

Add a Shortcode Block in your Page/Post: https://share.getcloudapp.com/kpuJNzpd

[portable_hook]

https://share.getcloudapp.com/ApuJGooG

Step 4

Use the hook. The hook should be Custom Hook and the Custom Hook Name should be hook_name: https://share.getcloudapp.com/eDu5xKLX

Hope this clarifies. :)

oh now got it so as in the screenshot I can add my advert code there also I don't need any ad inserter right?

Yes, that's correct. You may insert ads through this way instead of using an ad inserter. :)

OK, thanks a lot.

You’re welcome! Glad to be of assistance! :)

One last question I need to place the shortcode to all the posts manually right?

Yes. if you’re wanting to add the advertisements in specific and different places in your posts, this would be the best approach to have full control.

Otherwise, if your planning to add the advertisements say in after the first paragraph of all posts for instance, then using the code here instead would do:

add_shortcode('portable_hookb', function($atts){
	ob_start();
        $atts = shortcode_atts( array(
            'hook_nameb' => 'no foo'
        ), $atts, 'portable_hookb' );
		do_action($atts['hook_nameb']);
	return ob_get_clean();
});

add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
    global $post;
    $inserted_hook = do_shortcode('[portable_hookb hook_nameb="after_first_paragraph"]');
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $inserted_hook, 1, $content ) . $inserted_hook;
    }
    return $content;
}

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
  $closing_p = '</p>';
  $paragraphs = explode( $closing_p, $content );
  foreach ($paragraphs as $index => $paragraph) {
    if ( trim( $paragraph ) ) {
     $paragraphs[$index] .= $closing_p;
    }
    if ( $paragraph_id == $index + 1 ) {
     $paragraphs[$index] .= $insertion;
    }
  }
  return implode( '', $paragraphs );
}

add_filter( 'generate_hooks_list', function($hooks){
    $hooks['content']['hooks'][] = 'after_first_paragraph_and_content';
    return $hooks;
}, 99,1 ); 

See: https://share.getcloudapp.com/JruOqBjr

In this case, you wouldn’t need to add the shortcode manually. Using Custom hook after_first_paragraph would allow you to hook after the first paragraph of all posts with this code.

Hope this clarifies. :)

Got it thanks

This archived topic is closed to new replies.