- This topic has 15 replies, 4 voices, and was last updated 3 years, 5 months ago by
Fernando.
-
AuthorPosts
-
November 14, 2022 at 11:28 am #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!November 14, 2022 at 11:38 am #2415428Ying
StaffCustomer SupportIs it possible to insert these scripts via hooks or something within GeneratePress?
What kind of script are you referring to?
November 15, 2022 at 12:00 am #2415938Ivaylo
Ads
November 15, 2022 at 12:09 am #2415945Fernando 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
November 15, 2022 at 1:26 am #2416011Ivaylo
Thanks, I’m seeing after_content hook, but I don’t know how to make one after second image in my post content?
November 15, 2022 at 1:41 am #2416027Fernando 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 Hookand custom hook name toafter_second_image. This should place the ad after the second image.November 15, 2022 at 1:43 am #2416031Ivaylo
Where to add this PHP snippet – in functions.php?
November 15, 2022 at 1:47 am #2416037Ivaylo
I don’t have featured image shown in my posts if this helps.
November 15, 2022 at 1:49 am #2416039Ivaylo
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.
November 15, 2022 at 2:00 am #2416050Fernando 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?
November 15, 2022 at 2:05 am #2416058Ivaylo
I’m adding this in Posts only ( not on pages ).
Yes, the hook display rules are set to Posts – All Posts.November 15, 2022 at 2:30 am #2416079Ivaylo
Do I have to put tick on Execute PHP or Execute Shortcodes in Custom Hook settings? Tried with Execute PHP, nothing happened.
November 15, 2022 at 8:39 am #2416820Ivaylo
Fernando, David?
November 15, 2022 at 9:17 am #2416853David
StaffCustomer SupportHi 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_contentto insert ashortcodewhich injects ahookthat we then use an Element to insert the code.
Their method will just filterthe_contentNovember 15, 2022 at 10:47 am #2416965Ivaylo
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?
-
AuthorPosts
- You must be logged in to reply to this topic.