Site logo

[Support request] Hooks a button into a post before an H2 headline

Home Forums Support [Support request] Hooks a button into a post before an H2 headline

Home Forums Support Hooks a button into a post before an H2 headline

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2441482
    Frank

    Hi,

    I created a block in elements that contains a single button.

    How can I hook this block in all my posts before a particular H2 headline?
    For example, if I want to hook the button in directly before the second H2 headline of every post.

    Thank You,
    Frank

    #2441541
    David
    Staff
    Customer Support

    Hi there,

    there are no hooks inside you content, but you can filter the_content, to insert a shortcode which in turn outputs a Hook for you. Add this PHP Snippet to do that:

    
    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();
    });
    
    add_filter( 'the_content', 'insert_element_eleventh', 30 );
    function insert_element_eleventh( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_second_heading"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 2, $content );
        }
        return $content;
    }
    
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
      $closing_p = '</h2>';
      $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 );
    }

    It will create a custom hook called: after_second_heading

    You can change the hook name if you want on this line:
    $inserted_hook = do_shortcode('[portable_hook hook_name="after_second_heading"]');

    And this line the number 2 is where it sets which H2 to appear after:

    return prefix_insert_after_paragraph( $inserted_hook, 2, $content );

    #2441569
    Frank

    Hi, David

    Where should I add this php snippet?

    If I add it to the “Additional CSS” in Generate Press, will the after_second_heading hook appear in the list?

    Thanks,
    Frank

    #2441596
    David
    Staff
    Customer Support

    The code is PHP – this article explains how to add it to your site:

    https://docs.generatepress.com/article/adding-php/

    Then in your Block element, set the Hook to Custom and in the custom hook field paste in: after_second_heading

    #2441650
    Frank

    Hi, David

    That worked great!
    Can the code be changed to insert the block element BEFORE the second heading (before second H2)?

    Thanks,
    Frank

    #2442494
    David
    Staff
    Customer Support

    In the code:

    Remove this:

    
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
      $closing_p = '</h2>';
      $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 );
    }

    And add this:

    
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '<h2>';
        $paragraphs = explode( $closing_p, $content );
        if ($paragraphs) {
            $paragraphs[0] = $paragraphs[0].'<h2>'.$insertion;
        }    
        return implode( '<h2>', $paragraphs );
    }
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.