[Resolved] hooks related question

Home Forums Support [Resolved] hooks related question

Home Forums Support hooks related question

  • This topic has 10 replies, 2 voices, and was last updated 2 years ago by iamarghya.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2170595
    iamarghya

    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?

    #2170598
    Fernando
    Customer Support

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

    #2170600
    iamarghya

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

    #2170615
    Fernando
    Customer Support

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

    #2170616
    iamarghya

    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?

    #2170622
    Fernando
    Customer Support

    Yes, that’s correct. You may insert ads through this way instead of using an ad inserter. 🙂

    #2170623
    iamarghya

    OK, thanks a lot.

    #2170629
    Fernando
    Customer Support

    You’re welcome! Glad to be of assistance! 🙂

    #2170632
    iamarghya

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

    #2170661
    Fernando
    Customer Support

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

    #2170670
    iamarghya

    Got it thanks

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