[Resolved] Adding An Automatic Disclaimer To My Posts

Home Forums Support [Resolved] Adding An Automatic Disclaimer To My Posts

Home Forums Support Adding An Automatic Disclaimer To My Posts

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #2154629
    Jodie

    Hi folks,
    I’d like to have text for disclaimer with a link to the disclaimer page automatically applied below my first image on my posts. What’s the best way to do this please?
    Thanks for your help!

    #2154633
    Leo
    Staff
    Customer Support

    Hi Jodie,

    Take a look at this video. It should be very similar to what you are looking for:

    #2154688
    Jodie

    Hi Leo,

    Thanks for that. I’ve tried doing that but the priority doesn’t change the position of the aff disclaimer. The video mentioned something about the featured image also being added by a hook? Any tips? Thanks!

    Cheers Jodie

    #2154699
    Ying
    Staff
    Customer Support

    Hi Jodie,

    The priority doesn’t work because the image in your post is not a featured image but an image block manually added within the content.

    Unfortuanly there’s NO built-in hook within the content.

    Although you can try this solution to insert the disclaimer after the first paragraph:
    https://generatepress.com/forums/topic/element-after-first-paragraph/

    #2154703
    Jodie

    Thanks very much Ying. You mention featured image. What do you recommend there? Is it possible to add a hook for a featured image?

    #2154753
    Fernando
    Customer Support

    Hi Jodie,

    In reference to this Hooks Visual Guide, using after_entry_hook should place somewhere under the Featured image of the post.

    However, since you’re not using Featured images, as mentioned by Ying, you may create your own portable hook through a shortcode. This in turn will allow you to place Elements in any location you wish.

    For instance, you may add this 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.

    Then, after adding this PHP code, you may add the shortcode, portable_hook, manually to your preferred location.

    Example: https://share.getcloudapp.com/mXuYE7QA

    Also see: https://share.getcloudapp.com/OAu6RxvG

    Now you can hook onto this shortcode through a Custom hook, hook_name.

    See: https://share.getcloudapp.com/QwuLeQJo

    Through this, you may now place any Element in your preferred location. Just place the shortcode, and hook to this shortcode through the hook name you’ll specify.

    Kindly replace potable_hook and hook_name if you wish.

    Hope this clarifies. 🙂

    #2154767
    Jodie

    Thank you very much Fernando. In order for this to work, do I have to add the shortcode to every single post I want the disclaimer to appear? If so, is there a way to add it to all posts in one action?

    #2154785
    Fernando
    Customer Support

    With the code provided, you’ll need to add the shortcode to each post.

    If you want it automatically added after the first images of your Post, here is a code you may try:

    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_first_image"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_first_image( $inserted_hook, 1, $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 );
    }

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

    As mentioned, adding it through Code Snippets should work.

    Then, you may add an Element and use the hook for the code as such: https://share.getcloudapp.com/BluopBjp

    Specifically, use Custom Hook – after_first_image.

    Kindly let us know how it goes. 🙂

    #2154813
    Jodie

    Hi Fernando, I noticed there’s some code within a grey box and some out of it. We’re using a generatepress child theme.

    When I tried to add the below code to the functions.php file in my child theme, it showed an error.

    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_first_image"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_first_image( $inserted_hook, 1, $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 );
    }

    This is the error that came up.

    Your PHP code changes were rolled back due to an error on line 67 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again. syntax error, unexpected ‘<‘

    The error is in this line of code – $images = preg_split(‘/(<img[^>]+\>)/i’, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

    Can you help please?

    #2154827
    Fernando
    Customer Support

    Sorry, I missed placing everything in a code block which is causes issues if copied and pasted.

    I’ve edited the code I provided above, placing everything in a code block. Can you try it again?

    Kindly let us know how it goes. 🙂

    #2154846
    Jodie

    Hi Fernando, that worked, thank you so much!

    Cheers Jodie

    #2154848
    Fernando
    Customer Support

    You’re welcome Jodie! Glad that worked! Feel free to reach out if you’ll need further assistance with anything else. 🙂

    #2161379
    Jodie

    Hi Fernando,

    I want the disclaimer to display on a few pages as well as all my posts. I’ve got it set to display on those pages in the Hook location settings, but they’re not appearing on those pages. Do you know why this might be?

    Thanks!

    Cheers Jodie

    #2161460
    Fernando
    Customer Support

    To make the code work for pages as well rather than only for posts, you would need this code instead:

    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_first_image"]');
        if ( ( is_single() || is_page() ) && ! is_admin() ) {
            return prefix_insert_after_first_image( $inserted_hook, 1, $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 );
    }

    Moreover, make sure that the Display Rules of your Element is set to show in the specific pages you prefer as well.

    Hope this helps! Kindly let us know how it goes. 🙂

    #2161469
    Jodie

    Hi Fernando,

    Great, thank you so much! It worked.

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