[Resolved] HTML code hook within content

Home Forums Support [Resolved] HTML code hook within content

Home Forums Support HTML code hook within content

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1596605
    Danielle

    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!

    #1596725
    David
    Staff
    Customer Support

    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/

    #1597298
    Danielle

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

    #1597391
    David
    Staff
    Customer Support

    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.

    #1597477
    Danielle

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

    #1597578
    Elvin
    Staff
    Customer Support

    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.

    A wise man once said:
    "Have you cleared your cache?"

    #1597956
    Danielle

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

    #1598975
    Elvin
    Staff
    Customer Support

    Nice one. No problem. 🙂

    A wise man once said:
    "Have you cleared your cache?"

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