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.

Template for Affiliate Links

  • Originally the text was placed under the H2 Header + Text: https://imgur.com/a/DDxXpPA

    Braucht es frische Blütendolden?
    Ich habe frische Blütendolden nur als Deko verwendet. Die Creme habe ich mit fertigem Holunderblütensirup zubereitet. Dieses gibt es das ganze Jahr über zu kaufen, falls Du es nicht selber machst. Somit sind die weißen Sternchen nicht unbedingt notwendig. Doch zugegeben, sie verleihen der fertigen Torte einen besonderen Charme.

    Allerdings welken die Blüten sehr rasch. Sie sind dann nicht nur lasch, sondern sie werden auch schnell bräunlich. Von dem her bieten sie wirklich nur eine kurze Freude. Daher ist es in meinen Augen nicht schlimm, wenn man keine zur Hand hat. Dann musst Du Dir auch keine Gedanken über etwaige Läuse machen, die sich immer wieder einmal am Holunderbusch finden.

    Now for the text the block is showing before the comment section, but this is the wrong place. Maybe the solution is a shortcode?

    Greetings Kathrin

  • So … on all posts you want the Affiliate links to appear after the Last H2 Heading ?

  • In recipe posts the affiliate links should appear after the second h2 heading and the two p-tags. My gardening posts have a different structure. But why I can’t use the shortcode you have offered in the other post?

    Greetings Kathrin

  • OK, so if you want to manually add the shortcode, use this PHP:

    
    add_shortcode('hooky_affiliate_shortcode', function($atts, $content = null) {
        ob_start();
        do_action('affiliate_hook_inside_shortcode');
        return ob_get_clean();
    });
    

    Then in your element add the custom hook of: affiliate_hook_inside_shortcode
    And in your posts add the shortcode: [hooky_affiliate_shortcode]

  • Ok and what would have been your automated solution. Would it be possible to place the block after the second H2 and the two following p-tags? I thought that is a little bit to tricky as you mentioned in one of my last support threads.

    Greetings Kathrin

  • Automating is possible but it requires consistency in the layouts otherwise it gets too complicated.
    As you have different layouts you will need to manually add the shortcode

  • Thank you very much the shortcode works fine 🙂

    Ok, could we do it for the recipe category? Here every post looks the same.

    Greetings Kathrin

  • Use this PHP code to create a custom hook after the h2 followed by 2 paragraphs:

    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();
    });
    
    function insert_hook_after_h2( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_h2"]');
        
        if ( is_single() && ! is_admin() ) {
            $pattern = '/<h2[^>]*>.*?<\/h2>\s*<p[^>]*>.*?<\/p>\s*<p[^>]*>.*?<\/p>/s';
           $content = preg_replace_callback($pattern, function($match) use ($inserted_hook) {
                return $match[0] . $inserted_hook;
            }, $content, 1); 
        }
        return $content;
    }
    

    Then in your element, use custom hook >after_h2.

  • Wow! It works great, thank you very much!

    Do I need two elements one for recipes with the automated insertion and one for gardening that only works with the shortcode? Or is it possible to create a shortcode that also works with the same hook as the automated solution?

    Greetings Kathrin

  • The PHP creates the custom hook for all element types, you can try it for both post types.

  • Ok, so I don’t need that shortcode: `add_shortcode(‘hooky_affiliate_shortcode’, function($atts, $content = null) {
    ob_start();
    do_action(‘affiliate_hook_inside_shortcode’);
    return ob_get_clean();
    });`

    If I want to place the element manually per shortcode I use [hook >after_h2] in the post?

    Greetings Kathrin

  • Hmm, I would do it that way:

    First I create the custom fields.

    Than create one element that only shows on category recipes and another element that only shows on gardening posts – both using the same ACF Fields.

    Create one Snippet with the shortcode for manually placement and another snippet with the automated placement.

    Is this the wright way?

    Greetings Kathrin

  • Let’s take a step back, as this is becoming too complicated.

    So, has the automated insertion been sorted out?

    For manual insertions, you will need a separate shortcode for that.

  • The automated insertion should work for all postings in the recipe category and the manual insertion should work for all postings in the category gardening. The code provided from your collegues works fine. But I’m not sure, if I can use the same ACF custom fields for two elements.

    Greetings Kathrin

  • But I’m not sure, if I can use the same ACF custom fields for two elements.

    Yes, you can.

  • Ok, at the moment I have this setup, unfortunately nothing shows up this way:

    ACF Custom Field -> one time

    Element Gardening –> Custom Hook affiliate_hook_inside_shortcode –> Posts / All posts –> PHP Function:

    function db_affiliate_hook_inside_shortcode($atts, $content = null) {
          ob_start();
          do_action('custom_shortcode_hook');
          return ob_get_clean();
    }
    add_shortcode('hooky_affiliate_shortcode', 'db_affiliate_hook_inside_shortcode');

    Element Recipe –> Custom Hook after_h2 –> Posts / all posts –> PHP Function:

    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();
    });
    
    function insert_hook_after_h2( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_h2"]');
        
        if ( is_single() && ! is_admin() ) {
            $pattern = '/<h2[^>]*>.*?<\/h2>\s*<p[^>]*>.*?<\/p>\s*<p[^>]*>.*?<\/p>/s';
           $content = preg_replace_callback($pattern, function($match) use ($inserted_hook) {
                return $match[0] . $inserted_hook;
            }, $content, 1); 
        }
        return $content;
    }

    I have tried to set category post / gardening // category post / recipes but both doesn’t work. What should I do to get work?

    Thank you very much!
    Greetings Kathrin

Viewing 16 posts - 17 through 32 (of 39 total)
  • You must be logged in to reply to this topic.