[Resolved] Disabling featured Images from being pinned

Home Forums Support Disabling featured Images from being pinned

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1501432
    Janine

    Hiya.

    I want to prevent the featured images from being pinned.

    I tried to use these two bits of code, but neither worked for me:

    /**Disable pinning of all featured images.
    */
    add_filter( ‘post_thumbnail_html’, function( $html ){
    // Something already has ‘data-pin-nopin’, so we don’t need to modify.
    if ( false !== stripos( $html, ‘data-pin-nopin’ ) ) {
    return $html;
    }
    // Add ‘data-pin-nopin’ to the HTML tag.
    $html = str_replace( ‘<img ‘, ‘<img data-pin-nopin=“true” ‘, $html );
    return $html;
    });

    AND this…

    add_filter( ‘wp_get_attachment_image_attributes’, function ( $attr ) {
    $attr[‘data-pin-nopin’] = ‘true’;
    return $attr;
    } );

    The second function was said to work with GP, but it didn’t for me.

    How can I do this with GP?

    Thank you.

    #1501488
    Leo
    Staff
    Customer Support

    Hi there,

    The code looks good to me.

    Can you link me to the page in question with the code added using one of these methods?
    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Make sure to clear and disable your caching plugin if you are using any.

    #1501529
    Janine

    Hi, Leo.

    I created added the codes to Code Snippets. Have two snippets. Which one do you want me to use?

    Plus, I am doing this on a staging site. The link is the private information area.

    Thank you.

    #1501606
    Leo
    Staff
    Customer Support

    Try adding both to start.

    The page you linked requires a username and a password to view.

    Not sure where you got the code from but GP uses the default featured image option from WordPress, so if the code works in a twenty series WP theme, then it should work in GP.

    #1501631
    Janine

    Hi, Leo.

    What I want is visitors to only be able to pin the pins I design for pinning. I want all other images to be no pin.

    I have been trying to add data-pin-nopin=”true” individually to my images within the posts and pages. And my success has been inconsistent. It works on the homepage.

    BUT it is not working on other pages. When I enter the data-pin-nopin=”true”, it doesn’t stick even after I update. It also changes how the image displays on the backend.

    Can you help accomplish my goal?

    I enabled the two code snippets in the Code Snippets plugin.

    I got the codes from these two websites:

    https://tinyurl.com/yxanhqk8
    https://tinyurl.com/y6trwnor

    I have cleared and disabled caching plugins.

    #1501861
    Leo
    Staff
    Customer Support

    This code here should definitely work for the featured images in single posts:

    /**
     * Disable pinning of all featured images.
     */
    add_filter( 'post_thumbnail_html', function( $html ){
    // Something already has 'data-pin-nopin', so we don't need to modify.
    if ( false !== stripos( $html, 'data-pin-nopin' ) ) {
    return $html;
    }
    // Add 'data-pin-nopin' to the <img> HTML tag.
    $html = str_replace( '<img ', '<img data-pin-nopin="true" ', $html );
    return $html;
    });

    I just tested it using a fresh install and you can see it here:
    https://www.screencast.com/t/xgAvw1gUdy

    And this code here works for the featured images on the blog/archive pages:

    add_filter( 'wp_get_attachment_image_attributes', function ( $attr ) {
    $attr['data-pin-nopin'] = 'true';
    return $attr;
    } );

    I also tested this and you can see it here:
    https://www.screencast.com/t/iyMWMxeo

    I did notice that when I copied the code from the website, the quotations got a little funky so make sure you fix those or copy my code here.

    #1502066
    Janine

    Hi, Leo.

    Thank you! Your codes seem to be working.

    BE SAFE.

    #1502633
    Leo
    Staff
    Customer Support
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.