Site logo

[Support request] Add scripts ( ads ) using hooks?

Home Forums Support [Support request] Add scripts ( ads ) using hooks?

Home Forums Support Add scripts ( ads ) using hooks?

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #2415413
    Ivaylo

    Hi,
    I’m using Ad Inserter Pro to insert ads in my posts ( 1. After the second image in the content and 2. After the content ). Is it possible to insert these scripts via hooks or something within GeneratePress? Would be nice to remove one more plugin if the same functionality could be achieved via the theme.
    Thanks!

    #2415428
    Ying
    Staff
    Customer Support

    Is it possible to insert these scripts via hooks or something within GeneratePress?

    What kind of script are you referring to?

    #2415938
    Ivaylo

    Ads

    #2415945
    Fernando
    Customer Support

    Hi Ivaylo,

    Yes, it should be possible. You can try adding the Ad code through a Hook Element

    Reference: https://docs.generatepress.com/article/hooks-element-overview/

    You can also view this response by David showing how to reserve a space for your ad so that CLS is prevented: https://generatepress.com/forums/topic/reserve-space-for-ads-in-header-or-post/#post-1855257

    #2416011
    Ivaylo

    Thanks, I’m seeing after_content hook, but I don’t know how to make one after second image in my post content?

    #2416027
    Fernando
    Customer Support

    I see. Try adding this PHP snippet:

    add_shortcode('portable_hook', function($atts){
    	ob_start();
            $atts = shortcode_atts( array(
                'hook2_name' => 'no foo'
            ), $atts, 'portable_hook' );
    		do_action($atts['hook2_name']);
    	return ob_get_clean();
    });
    
    add_filter( 'the_content', 'insert_featured_image', 20 );
    function insert_featured_image( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook2_name="after_second_image"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_first_image( $inserted_hook, 3, $content );
        }
        return $content;
    
    }
    
    function prefix_insert_after_first_image( $insertion, $image_id, $content ) {
      $images = preg_split('/(<img[^>]+\>)/i', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
      foreach ($images as $index => $image) {
        if ( $image_id == $index ) {
         $images[$index] .= $insertion;
        }
      }
      return implode( '', $images );
    }

    Then, set your Hook to Custom Hook and custom hook name to after_second_image. This should place the ad after the second image.

    #2416031
    Ivaylo

    Where to add this PHP snippet – in functions.php?

    #2416037
    Ivaylo

    I don’t have featured image shown in my posts if this helps.

    #2416039
    Ivaylo

    Added the PHP to functions.php and created custom hook named after_second_image – not working. Tried with plain text instead of ad script, but I don’t see the text visualized after the second image, so I guess it won’t work with ad script either.

    #2416050
    Fernando
    Customer Support

    To clarify, you’re adding this in Posts right?

    Yes, you can add the PHP in functions.php.

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    How are you setting up the Hook Element? Have you set the Display rules?

    #2416058
    Ivaylo

    I’m adding this in Posts only ( not on pages ).
    Yes, the hook display rules are set to Posts – All Posts.

    #2416079
    Ivaylo

    Do I have to put tick on Execute PHP or Execute Shortcodes in Custom Hook settings? Tried with Execute PHP, nothing happened.

    #2416820
    Ivaylo

    Fernando, David?

    #2416853
    David
    Staff
    Customer Support

    Hi there,

    sorry form the delay – are you trying that code on the same site you’re running Ad Inserter on ?
    As theres a chance the two won’t work together. As we’re both trying to filter the content…

    Personal opinion, for this kind of thing i would keep using Ad Inserter for this kind of function.
    Although propably unnoticeable, the above method, which is rather hacky is probably less performant then the plugin.

    Our code is: filtering the_content to insert a shortcode which injects a hook that we then use an Element to insert the code.
    Their method will just filter the_content

    #2416965
    Ivaylo

    Thanks, David! I’m looking to optimize speed/performance and decrease the scripts that are being loaded. Are you telling me that ditching Ad Inserter and using 2 separate hooks will be heavier?

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