-
Topfgartenwelt
At the moment I’m adding the link for my newsletter with AdInserter. It always appears before the 2nd paragraph counted from below. Is there a way to use a hook for that, so that it is possible to style that link a little bit?
Thank you very much!
Greetings Kathrin
-
Hi there,
Creating a custom hook isn’t that easy, it would be easier just use CSS to style the link.
-
Topfgartenwelt
That isn’t possible with ADInserter.
I created a custom hook to show the featured image after the 1st paragraph. So I fought it should also work to show a custom hook before the 2nd paragraph counted from bottom.
The code provided by your colleagues was that one:
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(); }); add_filter( 'the_content', 'insert_featured_image', 20 ); function insert_featured_image( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_first_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 1, $content ); } return $content; } 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 ); }
Greetings Kathrin
-
Just to make sure you want the hook to be inserted before the 2nd last paragraph?
If so, change your code to this:
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(); }); add_filter( 'the_content', 'insert_featured_image', 20 ); function insert_featured_image( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="before_second_last_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_before_second_last_paragraph( $inserted_hook, $content ); } return $content; } function prefix_insert_before_second_last_paragraph( $insertion, $content ) { $paragraph_pattern = '/<p[^>]*>.*?<\/p>/s'; preg_match_all($paragraph_pattern, $content, $matches, PREG_OFFSET_CAPTURE); // Count the number of matches (paragraphs) $paragraph_count = count($matches[0]); if ($paragraph_count < 2) { // If there are less than two paragraphs, insert at the beginning return $insertion . $content; } // Find the position of the second-to-last paragraph $second_last_paragraph_position = $matches[0][$paragraph_count - 2][1]; // Insert content before the second-to-last paragraph $updated_content = substr($content, 0, $second_last_paragraph_position) . $insertion . substr($content, $second_last_paragraph_position); return $updated_content; }
-
Topfgartenwelt
Thank you very much! The hook name is now: before_second_last_paragraph?
Greetings Kathrin
-
Yes, correct 🙂
-
Topfgartenwelt
Hmm, I don’t see the hook: https://www.topfgartenwelt.com/omas-vanillekipferl-mit-mandeln
I have flushed the cash.
The snippet doesn’t work because of an error in line 12 and is automatically deactivated.
Greetings Kathrin
-
Topfgartenwelt
I got it work. Instead featured_image I took the name newsletter_hook.
Thank you very much!
Greetings Kathrin
-
You are welcome 🙂
- You must be logged in to reply to this topic.