- This topic has 10 replies, 2 voices, and was last updated 2 years ago by iamarghya.
-
AuthorPosts
-
March 28, 2022 at 8:12 pm #2170595iamarghya
I just read your hooks visual guide so I have a question… is there any way to hook an element before a certain paragraph inside the main content. Like I want to use some hook element that I want to place before a certain paragraph or photo is there any way?
March 28, 2022 at 8:20 pm #2170598Fernando Customer SupportHi Iamarghya,
If you want to place an Element somewhere specific not in the list, you may create your own shortcode which create a new custom hook:
Portable hook code:
function your_shortcode($atts, $content = null) { ob_start(); do_action('hook_name'); return ob_get_clean(); } add_shortcode('portable_hook', 'your_shortcode');
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
Adding it through Code Snippets should work.
After adding this PHP, you may add the shortcode as such: https://share.getcloudapp.com/ApuJGooG
Then you can now hook to
hook_name
through custom hook.Example: https://share.getcloudapp.com/eDu5xKLX
Hope this clarifies. 🙂
March 28, 2022 at 8:25 pm #2170600iamarghyaCan u just repeat the step once more, please , couldn’t understand
March 28, 2022 at 8:39 pm #2170615Fernando Customer SupportStep 1
Download the plugin Code Snippets through Plugins > Add new: https://share.getcloudapp.com/P8u1mKJg
Step 2
Add this PHP code through Snippets > Add new:
function your_shortcode($atts, $content = null) { ob_start(); do_action('hook_name'); return ob_get_clean(); } add_shortcode('portable_hook', 'your_shortcode');
Step 3
Add a Shortcode Block in your Page/Post: https://share.getcloudapp.com/kpuJNzpd
[portable_hook]
https://share.getcloudapp.com/ApuJGooG
Step 4
Use the hook. The hook should be Custom Hook and the Custom Hook Name should be
hook_name
: https://share.getcloudapp.com/eDu5xKLXHope this clarifies. 🙂
March 28, 2022 at 8:41 pm #2170616iamarghyaoh now got it so as in the screenshot I can add my advert code there also I don’t need any ad inserter right?
March 28, 2022 at 8:43 pm #2170622Fernando Customer SupportYes, that’s correct. You may insert ads through this way instead of using an ad inserter. 🙂
March 28, 2022 at 8:48 pm #2170623iamarghyaOK, thanks a lot.
March 28, 2022 at 9:04 pm #2170629Fernando Customer SupportYou’re welcome! Glad to be of assistance! 🙂
March 28, 2022 at 9:13 pm #2170632iamarghyaOne last question I need to place the shortcode to all the posts manually right?
March 28, 2022 at 10:31 pm #2170661Fernando Customer SupportYes. if you’re wanting to add the advertisements in specific and different places in your posts, this would be the best approach to have full control.
Otherwise, if your planning to add the advertisements say in after the first paragraph of all posts for instance, then using the code here instead would do:
add_shortcode('portable_hookb', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook_nameb' => 'no foo' ), $atts, 'portable_hookb' ); do_action($atts['hook_nameb']); return ob_get_clean(); }); add_filter( 'the_content', 'insert_featured_image', 20 ); function insert_featured_image( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hookb hook_nameb="after_first_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 1, $content ) . $inserted_hook; } 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 ); } add_filter( 'generate_hooks_list', function($hooks){ $hooks['content']['hooks'][] = 'after_first_paragraph_and_content'; return $hooks; }, 99,1 );
See: https://share.getcloudapp.com/JruOqBjr
In this case, you wouldn’t need to add the shortcode manually. Using Custom hook after_first_paragraph would allow you to hook after the first paragraph of all posts with this code.
Hope this clarifies. 🙂
March 28, 2022 at 10:44 pm #2170670iamarghyaGot it thanks
-
AuthorPosts
- You must be logged in to reply to this topic.