Site logo

Archived topic

Add scripts ( ads ) using hooks?

15 replies · Started by Ivaylo on November 14, 2022

Viewing posts 1–15 of 16

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!

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

What kind of script are you referring to?

Ads

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

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.

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

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

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.

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

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

Fernando, David?

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

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?

This archived topic is closed to new replies.