Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Can I put an element in the middle of my content?

  • Kristen Hubert

    Hi I want to use a hook to put an email sign up form part way through the body of my post content – e.g. 3-5 paragraphs in for example or after the second H2 or something like that.

    Is there a way to do that? I can’t find a location like that in any of the hook options.

  • Hi there,

    Hooks only exist in code, they aren’t inside your content.
    But it is possible to create a custom hook after X number of paragraphs.
    Here is the require PHP to create the Hook:

    
    add_filter( 'the_content', 'db_insert_portable_hook_two', 20 );
    function db_insert_portable_hook_two( $content ) {
        if ( is_single() && ! is_admin() ) {
            global $post;
            ob_start();
            do_action('inside_post_content');
            $inserted_hook = ob_get_clean();
            return prefix_insert_after_paragraph( $inserted_hook, 3, $content );
        }
        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 );
    }
    

    This line: return prefix_insert_after_paragraph( $inserted_hook, 3, $content ); the 3 specifies the paragraph number after to insert it.

    Then you can creata a block Element and in the Hook list choose: Custom Hook and paste: inside_post_content in the field ( thats the hook the code provides ).

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