Site logo

Archived topic

HTML code hook within content

7 replies · Started by Danielle on December 28, 2020

Viewing posts 1–8 of 8

Hi,

I would like to add my email signup embed form after a few paragraphes in every blog post via a hook. I currently have it in the bottom of my posts but I would like to see it within the first one-third of my blog posts instead.

Thanks!

Hi there,

Hooks only exist in the Theme Templates - not within the_content() of you post or pages.
If you want to insert content within the post content then we recommend using the Ad Inserter plugin - it doesn't just work with adverts, you can add any content you want including a shortcode for your form:

https://en-gb.wordpress.org/plugins/ad-inserter/

Is there a way I can do this without a plugin?

Aside of manually adding them to the page, you would need a PHP Snippet like this:

add_filter( 'the_content', 'insert_content_after_paragraphs', 20 );
function insert_content_after_paragraphs( $content ) {
    $html = do_shortcode( '[your-shortcode]' );
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $html, 3, $content );
    }
    return $content;
}
// Parent Function that makes the magic happen
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 );
}

The line: $html = do_shortcode( '[your-shortcode]' ); you need to replace your-shorcode with the forms shortcode. However, i cannot say whether this will work correctly with all shortcodes.

Thanks for your help! I don't have a shortcode for my Mailerlite forms, is there a way to implement the HTML code?

Hi,

Thanks for your help! I don’t have a shortcode for my Mailerlite forms, is there a way to implement the HTML code?

Mailerlite plugin generates shortcode of something like this [mailerlite_form form_id=1] where 1 is the ID of your form.

Reference:
http://help.mailerlite.com/article/show/85637-how-to-insert-a-mailerlite-signup-form-on-my-wordpress-site

But to directly answer the question:

Thanks for your help! I don’t have a shortcode for my Mailerlite forms, is there a way to implement the HTML code?

While we recommend sticking w/ the shortcode, you can do this by changing the line $html = do_shortcode( '[your-shortcode]' ); to $html = 'your HTML code here' but as David mentioned, its pretty uncertain if it will work properly.

Thanks so much for your help! I managed to find the shortcode and it works like a charm!

Nice one. No problem. :)

This archived topic is closed to new replies.